简体   繁体   English

ngRepeat筛选器,按数组属性

[英]ngRepeat filter by array-property

If I have an object with a property that is an array, how can I filter by the first element of this property? 如果我有一个对象,该对象的属性是数组,那么如何按该属性的第一个元素进行过滤?

Data 数据

{
    Id: "0",
    Name: ["John", "Doe"]
}

ngRepeat ngRepeat

<li ng-repeat="u in users | filter:{Name[0]: 'John'}">{{ u.Name[0] + ' ' + u.Name[1] }}</li>

Slightly modified from this SO answer to work with arrays: 这个SO答案稍作修改即可使用数组:

<li ng-repeat="u in users | filter: {Name: ['John']}">{{ u.Name[0] + ' ' + u.Name[1] }}</li>

Update: This doesn't seem to work after version 1.2.1 or so. 更新: 1.2.1以后的版本似乎不起作用。 You can see the difference in this fiddle using v1.2.1 and this one using v1.4.8 . 您可以使用v1.2.1v1.4.8看到这种提琴的 区别 Not sure what's changed between these two or if the feature was ever even officially supported. 不知道这两个之间有什么变化,或者不确定该功能是否曾被正式支持。 As an alternative, you could use a function predicate, like so: 或者,您可以使用函数谓词,如下所示:

<li ng-repeat="u in users | filter: isJohn">{{ u.Name[0] + ' ' + u.Name[1] }}</li>

controller: 控制器:

$scope.isJohn = function(value){
    return value.Name[0] === 'John';
};

But then again, that's not much simpler than creating a custom filter like nubinub suggested in the comments, and definitely less reusable. 但是再说一次,这并不比创建注释中建议的自定义过滤器(如nubinub)简单得多,而且绝对不太可重用。

尝试这个

<li ng-repeat="u in users | filter:u.Name[0] == 'John'">{{ u.Name[0] + ' ' + u.Name[1] }}</li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM