<!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操作頁面背景變暗
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com