具有多个烫发的 AngularJS 过滤器 [英] AngularJS filter with multiple perms

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

问题描述

我有这样的用户

[{id:1,name:'name',parent_id:0,type:1},{id:2,name:'name2',parent_id:0,type:2},{id:3,name:"name1 child",parent_id:1,type:1}]

我正在尝试根据 parent_id 和类型 id 显示父母和孩子用户,例如 id 3 是 id 1 的孩子,我试图像这样显示他们

http://jsfiddle.net/U3pVM/689/

感谢帮助

解决方案

我修好了你的小提琴:

http://jsfiddle.net/eQP8S/

 

<ol><li ng-repeat="user in users | filter:{parent_id:0}">{{用户名}}<ol><li ng-repeat="child in users | filter:{parent_id:user.id, type:user.type}">{{child.name}}</ol></ol>

您的主要问题是您试图将过滤器"过滤器与一个函数一起使用,这没问题,但该函数不允许采用任何参数.如果您确实需要创建一个带参数的过滤器,您必须编写一个自定义过滤器:http://docs.angularjs.org/guide/dev_guide.templates.filters.creating_filters.

但在您的情况下,您可以使用标准的过滤器"过滤器并将其传递给可以执行您想要的操作的对象.

另外,诅咒你们 Angular 开发人员创建了一个名为 filter 的抽象,然后创建了一个过滤器"过滤器.我的意思是,这有多混乱?

i have a users like this

[{id:1,name:'name',parent_id:0,type:1},
{id:2,name:'name2',parent_id:0,type:2},
{id:3,name:"name1 child",parent_id:1,type:1}]

and i am trying to display parents and child users based on parent_id and type id so for example id 3 is child of id 1 and i am trying to display them like this

http://jsfiddle.net/U3pVM/689/

thanks for help

解决方案

I fixed up your fiddle:

http://jsfiddle.net/eQP8S/

 <div ng-controller="MyCtrl">
    <ol>
    <li ng-repeat="user in users | filter:{parent_id:0}">
         {{user.name}}
        <ol>
               <li ng-repeat="child in users | filter:{parent_id:user.id, type:user.type}">
                   {{child.name}}
               </li>
        </ol>
    </li>
   </ol>
</div>

Your main problem was that you were trying to use the "filter" filter with a function, which is okay, but that function isn't allowed to take any parameters. If you actually need to create a filter that takes parameters you have to write a custom filter: http://docs.angularjs.org/guide/dev_guide.templates.filters.creating_filters.

But in your case, you can use the standard "filter" filter and pass it objects that will do what you want.

Also, curse you Angular developers for creating an abstraction called filter and then creating a "filter" filter. I mean, how confusing is that?

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

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