角NG重复使用条件 [英] angular ng-repeat with condition

查看:139
本文介绍了角NG重复使用条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么做这样的事情的角度与NG-重复?
我将使用从inits与2位朋友,阵列中的文档的例子,如果我想只能做对于那些26岁以上的所有朋友重演?

 <!DOCTYPE HTML>
< HTML NG-应用>
< HEAD>
&所述; SCRIPT SRC =HTTP://$c$c.angularjs.org/1.0.6/angular.min.js>&下; /脚本>
< /头>
<身体GT;
< D​​IV NG-的init =朋友= [{名称:'约翰',年龄:25},{名称:'玛丽',年龄:28}]>
我有{{friends.length}}朋友。他们是:
< UL>
<李NG重复=朋友的朋友>
[{{$指数+ 1}}] ​​{{friend.name}}谁是{{friend.age}}岁。
< /李>
< / UL>
< / DIV>
< /身体GT;
< / HTML>


解决方案

创建自定义过滤器。

HTML

 < HTML NG-应用=someApp>

 <李NG重复=,在朋友的朋友|时代>

JS:

  VAR someApp = angular.module('someApp',[]);someApp.filter('年龄',函数(){
    返回功能(输入,大写){
        变量超出= [];
        对于(VAR I = 0; I< input.length;我++){
            如果(输入[I]。年龄> = 26){
                out.push(输入[I]);
            }
        }
        返回的;
    }
});

How do you do something like this in angular with ng-repeat? I'll be using the example from the documentation which inits an array with 2 friends, what if I wanted to only do a repeat for all friends that are 26 and older?

    <!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>

解决方案

Create a custom filter.

HTML:

<html ng-app="someApp">

and

<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重复使用条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆