Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了AngularJs Filter自定义过滤器控制ng-repeat去除重复大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

代码

<div ng-app="myApp" ng-controller="myCtrl">
    <p ng-repeat="x in items | unique:'id'">
        {{x.iD}}---{{x.namE}}
    </p>
</div>
<script>
    //AngularJs 自定义过滤器
    //1.使用过滤器,去除重复
    angular.module('common',[]).filter('unique',function () {
        return function (collection,keyName) {
            console.info(collection);
            console.info(keyName);
            var output = [],keys = [];
            angular.forEach(collection,function (item) {
                var key = item[keyname];
                if (keys.indexOf(key) === -1) {
                    keys.push(key);
                    output.push(item);
                }
            });
            return output;
        }
    });
    var app = angular.module('myApp',['common']);
    app.controller('myCtrl',function ($scope) {
        //$scope.items = [1,2,3,2];
        //当前unique 的过滤只针对,对象数组过滤
        $scope.items = [
            { id: 1,name: 'zhangsan' },{ id: 2,name: 'lisi' },{ id: 1,];
    });
</script>
结果:

大佬总结

以上是大佬教程为你收集整理的AngularJs Filter自定义过滤器控制ng-repeat去除重复全部内容,希望文章能够帮你解决AngularJs Filter自定义过滤器控制ng-repeat去除重复所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。