简体   繁体   English

如何在角度访问过滤数组的属性

[英]How to access property of filtered array in angular

I have an array and I want to filter to get to a record that matches some other value so I'm doing this: 我有一个数组,我想过滤以获得与其他值匹配的记录,所以我这样做:

jobsCtrl.matchList | filter: {job_id: job.id}

Where the controller's matchList looks like this: 控制器的matchList如下所示:

[{ job_id: 1, prop: 5},{job_id: 2, prop: 10 } ... ]

If I just output it like this it works: { job_id: 1, prop: 5} 如果我只是这样输出它,则可以正常工作: { job_id: 1, prop: 5}

But I want to access the prop property in the DOM, I would expect this to work: 但是我想访问DOM中的prop属性,我希望它能起作用:

{{ (jobsCtrl.matchList | filter: {job_id: job.id}).prop }}

But that just reads blank, is there a way to do this? 但这只是空白,有没有办法做到这一点?

Thanks! 谢谢!

Since filter returns an array you can't directly access an object property of array. 由于filter返回一个数组,因此您无法直接访问array的object属性。

You can however return the first element of the array and get the value of it's property 但是,您可以返回数组的第一个元素并获取其属性的值

{{ (jobsCtrl.matchList | filter: {job_id: job.id})[0].prop }}

You wouldn't want to use this too much though, such as inside ng-repeat as it would be quite expensive . 不过,您不想使用太多,例如在ng-repeat内部,因为它会非常昂贵。 Keep in mind that digests may run multiple times each scope change 请记住,每个范围更改都会使摘要运行多次

DEMO DEMO

Would this approach work for you? 这种方法对您有用吗?

<div ng-repeat="job in jobsCtrl.matchList | filter: {job_id: job.id}">
    <p>{{job.prop}}</p>
</div>

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

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