zoukankan      html  css  js  c++  java
  • angular中ng-repeat去重

    [html] view plain copy print?在CODE上查看代码片派生到我的代码片
    <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, name: 'zhangsan' },  
            ];  
        });  
    </script> 
    

      

  • 相关阅读:
    js克隆
    一些笔试题
    js的严谨模式
    Docker之Compose服务编排
    最近5年183个Java面试问题列表及答案[最全]
    Rancher概述
    迭代器和生成器
    Prometheus 安装
    Prometheus 介绍
    装饰器函数
  • 原文地址:https://www.cnblogs.com/zipon/p/5815237.html
Copyright © 2011-2022 走看看