角度过滤 [英] Filtering in angular

查看:62
本文介绍了角度过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供有关如何在Angular中重新编写以下代码的帮助吗?我在处理过滤器时遇到问题

Can someone help on how to re-write the below code in Angular. I am having problem in handling filters

ng-repeat ="myController.data中的数据|过滤器:{filterFlag:'true'}"

ng-repeat="data in myController.data | filter:{filterFlag:'true'}"

推荐答案

AngularJS

假设您的myController中有一个对象数组(在本例中为data),即

Lets say you have an array of objects (in your case named data) in your myController i.e.

this.data = [
{id: 1, name: 'Oscar', age: 36},
{id: 2, name: 'Nina', age: 36},
{id: 3, name: 'Alex', age: 39},
]

您想过滤掉36岁的人,可以在模板中对其进行迭代,如下所示:

And you want to filter out people with the age 36, you could iterate over it in the template like this:

<div ng-repeat="data in myController.data | filter:{age:36}">
{{data}}
</div> 

角度

在任何情况下,过滤器都很昂贵.这就是为什么它们被认为是不好的做法,也就是为什么不建议您在Angular中使用它的原因.样式指南说,您应该过滤掉控制器中的对象,而不是模板中的对象.您可能要考虑在AngularJS中进行相同的操作.您可以编写一个可以完成此任务的方法.

In any case, filters are expensive. Thats why they are considered bad practice, thats why you are not recommended to use it in Angular. The style guide says, you ought to filter out the objects in the controller, rather than in the template. You might want to consider doing the same in AngularJS. You could write a method which does the job.

有关更多信息,请访问... ng-repeat:按单个字段过滤

For more go to ... ng-repeat :filter by single field

这篇关于角度过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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