angularJs独特的过滤器 - 没有得到独特的数组长度独特、数组、过滤器、长度

2023-09-13 05:14:57 作者:巭孬嫑莪

https://github.com/a8m/angular-filter/

我使用上述Angularjs过滤库使用在NG-重复独特的属性。

I am using the above Angularjs-filter library to use unique property in ng-repeat.

下面是我的code

<div class="rankblock" ng-repeat="item in sports.Ranks.Rank  |unique:'_position'>
<span> {{sports.Ranks.Rank.length}}</span> //
</div>

后独特的NG-重复我排行数组长度变为6,但它仍然显示在输出9(原编曲长度)。

after unique in ng-repeat my Rank array length becomes 6 but it still shows 9 (the original Arr length) in the output.

推荐答案

过滤器内部返回到 NG-重复 A的新阵列的。这是过滤器的想法 - 过滤,但避免改变输入。换句话说:

The filter internally returns into ng-repeat a new array. This is the idea of the filter - to filter but avoid changing the input. In other words:

ng-repeat="item in sports.Ranks.Rank  | unique:'_position'

表示NG-重复要经过过滤功能,这是没有重复一个新的​​数组,当这里

says ng-repeat to go through the result of the filter function, which is a new array without duplicates, when here

<span> {{sports.Ranks.Rank.length}}</span>

你还是读了原来的长度。

you still read the length of the original one.

如果你想显示新的数组的长度可以做如请尝试以下

If you want to display the new array length you can do e.g. try the following

{{ (sports.Ranks.Rank | unique:'_position').length }}

(也永远不会做这样的事情,也许这工作),还是这不是一个高性能的解决方案。其实我会做的是在控制器级重复数据删除。

(did never do things like that, maybe this works), still this is not a performant solution. In fact what I would do is deduplication on a controller level.