角度ng-重复条件

J.P*_*Pip 13 javascript angularjs angularjs-directive

你怎么用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>
Run Code Online (Sandbox Code Playgroud)

Alw*_*ner 25

创建自定义过滤器.

HTML:

<html ng-app="someApp">
Run Code Online (Sandbox Code Playgroud)

<li ng-repeat="friend in friends | age">
Run Code Online (Sandbox Code Playgroud)

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;
    }
});
Run Code Online (Sandbox Code Playgroud)


mys*_*boy 15

你可以使用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>
Run Code Online (Sandbox Code Playgroud)


Vin*_*uis 7

这是工作小提琴

   $scope.call = function(a){

    if (a.age > 26)
        return true;
    else
        return false;

}
Run Code Online (Sandbox Code Playgroud)