时间:2021-07-01 10:21:17 帮助过:23人阅读
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ng-repeat去除重复</title> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <body> <p ng-app="myApp" ng-controller="myCtrl"> <p ng-repeat="x in items | unique:'id'"> {{x.id}}---{{x.name}} </p> </p> <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> </body> </html>
运行结果:
相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
推荐阅读:
highslide.js插件项目实战(附代码)
JS操作页面背景变暗
以上就是Filter自定义过滤器去重(附代码)的详细内容,更多请关注Gxl网其它相关文章!