对于其中一个属性是不确定的NG-重复过滤器 [英] ng-repeat filter for where an attribute is undefined

查看:133
本文介绍了对于其中一个属性是不确定的NG-重复过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这将是棱角分明非常简单。

我有翻译人员和其他用户的列表

  [{用户:'A',语言:'法国','英语','西班牙']},
{用户:'B'}]

然后我有一个NG重复,我只想显示谁拥有语言的用户。

  NG-重复=翻译用户|过滤器:{}语言

但不幸的是,我似乎无法弄清楚如何通过一个属性的存在进行筛选。
我已经试过所有方式

  {语言:'!未定义'}
{languages​​.length> 0}
{languages​​.length:0!'}

我不希望有写这个外部滤波器,因为我敢肯定,这是可能的角度,我只是没有能够得到正确的行话。


解决方案

 < D​​IV NG重复=翻译用户|过滤器:{语言:'! }>

说明:

考虑如下的列表:

  $ scope.items = [
    {
        propertyOne:'A',
        propertyTwo:'你好'
    },
    {
        propertyOne:'B',
        propertyTwo:空
    },
    {
        propertyOne:'C',
        propertyTwo:未定义
    },
    {
        propertyOne:'D'
    }
];

'!筛选出具有某种属性或具有某种属性,是不是未定义或为空对象的对象:

 <在项目DIV NG-重复=项|过滤器:{propertyTwo:'!! }>
    {{item.propertyOne}}
< / DIV>
!< - 输出:一个 - >

过滤掉没有一定的财产或具有某种属性,该属性未定义或空对象的对象:

 <在项目DIV NG-重复=项|过滤器:'!':{propertyTwo }>
    {{item.propertyOne}}
< / DIV>
< - 输出:B C D - >

Plnkr

请注意:它不会对性能与价值如0,虚假或空字符串工作('')

编辑:previous答案是从plunkr例子不同。我固定他们通过交换'!和'!'。

I was hoping this would be really simple in angular.

I have an list with translators and other users

[{user: 'a',languages:['french','english','spanish']},
{user: 'b'}]

I then have an ng-repeat where I only want to display the users who have languages

ng-repeat="translator in users | filter: {languages}"

but unfortunately, I can't seem to figure out how to filter by the existence of an attribute. I've tried all manner of

{languages:'!undefined'}
{languages.length > 0}
{languages.length:'!0'}

I don't want to have to write an external filter for this as I'm sure it's possible in angular, I'm just not able to get the lingo right.

解决方案

<div ng-repeat="translator in users | filter : { languages : '!!' }">

Explanation:

Consider a list as below:

$scope.items = [
    {
        propertyOne: 'a',
        propertyTwo: 'hello'
    },
    {
        propertyOne: 'b', 
        propertyTwo: null
    },
    {
        propertyOne: 'c', 
        propertyTwo: undefined
    },
    {
        propertyOne: 'd'
    }
];

'!!' filters out objects having a certain property or objects having a certain property that is not undefined or null:

<div ng-repeat="item in items | filter : { propertyTwo : '!!' }">
    {{item.propertyOne}}
</div>
<!-- outputs: a -->

'!' filters out objects not having certain property or objects having a certain property that is undefined or null:

<div ng-repeat="item in items | filter : { propertyTwo : '!' }">
    {{item.propertyOne}}
</div>
<!-- outputs: b c d -->

Plnkr

Note: It does not work for properties with values like 0, false or empty string('').

EDIT: previous answer was different from the plunkr example. I fixed them by swapping '!!' and '!'.

这篇关于对于其中一个属性是不确定的NG-重复过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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