gpt4 book ai didi

javascript - 使用来自 Angularjs 指令的数据过滤 ng-repeat 表

转载 作者:行者123 更新时间:2023-12-03 11:12:49 25 4
gpt4 key购买 nike

我有一个在 ng-repeat 循环中使用的对象数组,其中的属性通过 id 引用另一个对象。

{Description: 'something', OtherObjectId: 1}...

我正在使用指令来显示有关该 ng-repeat 循环中的 OtherObject 的信息。

<li ng-repeat="object in objects">
<div>{{object.Description}}</div>
<div other-object-info="date" other-object-id="{{object.OtherObjectId}}"></div>
</li>

otherObjectInfo指令使用innerHtml将请求的信息放入该html标签中

现在是明显不起作用的部分,我需要能够使用所有显示的信息(包括指令插入的信息)过滤该列表。

你会如何解决这个问题?我想最好的方法是在渲染 View 之前将 OtherObject 包含在对象数组中,但我喜欢使用该指令的简单方法,因为我需要在多个 View 中使用它。

非常感谢您的输出!

最佳答案

考虑为 ng-repeat 使用自定义过滤器。您可以将其他对象与数组分开,并将所需的任何内容传递给自定义过滤器函数:

<li ng-repeat="object in objects | filter:customFunct(otherObjectCollection)">

$scope.customFunct = function(otherCollection) {
return function(object) {
// locate the target object within otherCollection using object.OtherObjectId
// eg you can use this if you're using lodash:
var targetObj = _.find(otherCollection, {id : object.OtherObjectId});
// make the comparison you need
// use 'return' to return true based on the comparison
return targetObj.value > 40;
}
}

通过这种方式,您可以定义并使用带有传递参数的自定义过滤器。

您可以在此处阅读有关此解决方案的更多讨论:

https://stackoverflow.com/a/17813797/1220172

https://stackoverflow.com/a/17811582/1220172

关于javascript - 使用来自 Angularjs 指令的数据过滤 ng-repeat 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27487855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com