AngularJS - 在NG-重复未定义的属性过滤器? [英] AngularJS - filter for undefined properties in ng-repeat?

查看:163
本文介绍了AngularJS - 在NG-重复未定义的属性过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我AngularJS项目(V1.2.3),我的路线列表,我试图建立从对象的导航栏。我想要做的是显示与任何对象未定义 isRight 物业于一体的风格,并在财产是另一个定义。

For my AngularJS project (v1.2.3), I have a list of routes and am trying to build a navigation bar from the object. What I want to do is display any object with an undefined isRight property in one style, and where the property is defined in another.

在一个 NG-重复我想用一个未定义的 isRight 属性过滤这些对象。我怎样才能做到这一点的 NG-重复属性中,而无需借助于创建自定义过滤器功能?

In one ng-repeat I would like to filter those objects with an undefined isRight property. How can I accomplish this inside the ng-repeat attribute, without having to resort to creating a custom filter function?

$scope.nav = [
    { path: '/', title: 'Home' },
    { path: '/blog', title: 'Blog' },
    { path: '/about', title: 'About' },
    { path: '/login', title: 'Login', isRight: true }
];

我知道我可以只添加属性 isRight:假来的每个对象,或对右侧和左侧的链接,以及其他类似简单的解决办法单独的导航对象,但我很好奇,如果有与当前的结构来实现这一点的方式,使用的东西线沿线的:

I realize I could just add the attribute isRight: false to each object, or have separate nav objects for right and left side links, and other such simple workarounds, but I am curious if there is a way to accomplish this with the current structure, using something along the lines of:

<li ng-repeat="link in nav | filter:{isRight:undefined}">

这是一个多需要的好奇心,但我AP preciate任何建议。

This is more a curiosity than a need, but I appreciate any suggestions.

推荐答案

您可以否定一个过滤器前pression。因此,而不是处理未定义你可以过滤掉任何东西,其中 isRight 不是()真。像这样的:

You can negate a filter expression. So instead of dealing with undefined you can just filter out anything where isRight is not (!) true. Like this:

<li ng-repeat="link in nav | filter:{isRight:'!true'} ">

而对于相反的就可以了,当然,这样做:

And for the opposite you can, of course, do:

<li ng-repeat="link in nav | filter:{isRight:'true'} ">

演示小提琴

这篇关于AngularJS - 在NG-重复未定义的属性过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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