我有一個數組,其中假設有5個值存在示例(1,2,3,4,5); 現在對這個數組執行一些操作後,現在我的數組變成(1,2,3,4,5,1,2,3,4,5)現在我想用ng-repeat與$ index並且希望重複的數據將只顯示一次。有可能嗎?
-1
A
回答
1
您必須創建一個自定義過濾器以從列表中刪除重複項。它可以像下面
app.filter('unique', function() {
return function(list) {
var unique = function(origArr) {
var newArr = [],
origLen = origArr.length,
found, x, y;
for (x = 0; x < origLen; x++) {
found = undefined;
for (y = 0; y < newArr.length; y++) {
if (origArr[x] === newArr[y]) {
found = true;
break;
}
}
if (!found) {
newArr.push(origArr[x]);
}
}
return newArr;
};
return unique(list);
}
});
,然後用NG-重複使用它
<p ng-repeat="item in list | unique">List Item: {{ item }}</p>
請參閱本plnkr例如https://plnkr.co/edit/wklSOYJpHZxFlzCNPI9L?p=preview
+0
謝謝工作很好.... –
1
只需創建一個角度濾波得到獨特的項目 - JSFiddle Reference
var app = angular.module("app", []);
app.controller("mainCtrl", function($scope) {
$scope.items = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6];
});
app.filter("uniqueItem", function() {
return function(collection) {
var output = [];
angular.forEach(collection, function(item) {
if (output.indexOf(item) === -1) {
output.push(item);
}
});
return output;
};
});
<div ng-repeat="item in items | uniqueItem">List Item: {{ item }}</div>
相關問題
- 1. 使用getElementById()獲取ngrepeat $ index
- 2. ng-repeat track by $ index中斷圖像字段排序
- 3. 如何使用由ng-repeat track by $ index創建的量角器選擇元素?
- 4. ng-animate在ng-repeat中使用'track by $ index'時動畫錯誤的項目
- 5. 使用分號,過濾和排序不起作用的Angular「track by $ index」
- 6. 角度選擇'track by'重置選中
- 7. Angular 1.5「track by」Ruines binding in ng-options
- 8. ngRepeat在angularjs指令內的$ index
- 9. cqengine cant index by equals
- 10. AngularJS filter ng-by index?
- 11. python group by two columns,extract one index by one index
- 12. 基於選擇的ngRepeat
- 13. 如何與ngrepeat
- 14. Angular ngRepeat增加$ index並相應地渲染視圖。
- 15. AngularJS(ng-grid)order by original index
- 16. 顏色/ marker by index seaborn scatterplot
- 17. jquery select section by it's own index
- 18. Doctrine2 INDEX By Array格式
- 19. 與v-for一起使用track-by屬性有什麼意義?
- 20. 當使用帶有過濾器的'track by $ index`時,我出現錯誤。如何解決這個問題?
- 21. 「git branch -track」vs「git checkout -track」
- 22. ORDERBY不排序在以下ngRepeat表項
- 23. AngularJS ngRepeat Watchers Paradox
- 24. Angular.js指令transclude ngRepeat - 錯誤
- 25. Vue JS列出項目v-repeat track-by屬性不起作用
- 26. Angular ng-options with track by創建未知選項元素
- 27. AngularJS中的'track by'會導致「10 $ digest()interations」錯誤
- 28. 使用「track by」映射angularJS ui.select中的對象
- 29. Sphinx + Group By返回「index rt:group-by屬性找不到」
- 30. ng-show/hide with'track by $ index'不按索引隱藏
你想只顯示1,2,3,4,5嗎? – user7