[英]angular ng-repeat with condition
你怎么用ng-repeat做角度這樣的事情? 我將使用文檔中的示例,其中包含2個朋友的數組,如果我只想為所有26歲及以上的朋友重復一次,該怎么辦?
<!doctype html>
<html ng-app>
<head>
<script src="http://code.angularjs.org/1.0.6/angular.min.js"></script>
</head>
<body>
<div ng-init="friends = [{name:'John', age:25}, {name:'Mary', age:28}]">
I have {{friends.length}} friends. They are:
<ul>
<li ng-repeat="friend in friends">
[{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
</li>
</ul>
</div>
</body>
</html>
創建自定義過濾器。
HTML:
<html ng-app="someApp">
和
<li ng-repeat="friend in friends | age">
JS:
var someApp=angular.module('someApp', []);
someApp.filter('age', function() {
return function(input, uppercase) {
var out = [];
for (var i = 0; i < input.length; i++) {
if(input[i].age >= 26){
out.push(input[i]);
}
}
return out;
}
});
你可以使用ng-show =“friend.age> = 26”與朋友中的ng-repeat =“朋友”。它只會顯示年齡大於等於26的朋友。
<body>
<div ng-init="friends = [{name:'John', age:25}, {name:'Mary', age:28}]">
I have {{friends.length}} friends. They are:
<ul>
<li ng-repeat="friend in friends" ng-show="friend.age >=26">
[{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
</li>
</ul>
</div>
</body>
這是工作小提琴
$scope.call = function(a){
if (a.age > 26)
return true;
else
return false;
}
我在ng-repeat
使用了一個過濾器:
<div ng-repeat="product in products | filter:{ colour: by_colour }">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.