简体   繁体   English

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

[英]Filter ng-repeat table with data coming from directives Angularjs

I have an array of object that I use in a ng-repeat loop with an attribute referring to an another object by it's id. 我在ng-repeat循环中使用了一个对象数组,该对象的属性通过其ID引用另一个对象。

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

I'm using a directive to display information about the OtherObject in that ng-repeat loop. 我正在使用指令在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>

The otherObjectInfo directive use innerHtml to put the requested info in that html tag otherObjectInfo指令使用innerHtml将请求的信息放入该html标记中

Now is the part that doesn't obviously work, I need to be able to filter that list with all the displayed informations, including the one inserted by the directives. 现在这是显然不起作用的部分,我需要能够使用所有显示的信息(包括指令插入的信息)过滤该列表。

How would you resolve it? 您将如何解决? I guess the best way would be to include the OtherObject inside the objects array before rendering the view, but I like the simple way of using that directive because I need it in multiple views. 我猜最好的方法是在呈现视图之前将OtherObject包含在对象数组中,但是我喜欢使用该指令的简单方法,因为我需要在多个视图中使用它。

Thank you very much for your output! 非常感谢您的输出!

Consider a custom filter for the ng-repeat. 考虑一个用于ng-repeat的自定义过滤器。 You would keep the other objects separate from the array and pass whatever you need to the custom filter function: 您可以将其他对象与数组分开,然后将所需的任何内容传递给自定义过滤器函数:

<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;
    }
}

In this way, you can define and use a custom filter with a passed argument. 这样,您可以定义和使用带有传递参数的自定义过滤器。

You can read more discussion on this solution here: 您可以在此处阅读有关此解决方案的更多讨论:

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

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

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

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