Angular过滤器定义中的"anyPropertyKey"是什么? [英] What is 'anyPropertyKey' in Angular filter definition?
问题描述
Angular JS
{{ filter_expression | filter : expression : comparator : anyPropertyKey}}
我无法理解最后一个参数,即anyPropertyKey.有人可以用简单的例子
I am unable to understand the last argument i.e., anyPropertyKey. Can anybody please explain me with a simple example
<div ng-init="ar=[{n:'ram1', m:1}, {n:'mah', m:1}, {n:'vij', m:3}]">
<div ng-repeat='x in ar | filter: "1": false: propertyName'>{{x}}</div>
</div>
这是我到目前为止尝试过的.在上面的代码中,我应该写些什么而不是propertyName.
this is what I tried so far. In the above code what I should write instead of propertyName.
推荐答案
anyPropertyKey
与过滤器的表达式有关. 文档的相关部分是:
The anyPropertyKey
is related to the expression of the filter. The relevant part of the documentation is:
可以使用特殊的属性名称(默认情况下为$)(例如,如{$: "text"})接受与该对象或其任何属性的匹配 嵌套对象的属性.相当于简单的子字符串 与上述字符串匹配.特殊属性名称可以 使用anyPropertyKey参数将其覆盖.
A special property name ($ by default) can be used (e.g. as in {$: "text"}) to accept a match against any property of the object or its nested object properties. That's equivalent to the simple substring match with a string as described above. The special property name can be overwritten, using the anyPropertyKey parameter.
所以这里的第一件事就是这个表达式:
So the first thing here is that this expression:
<div ng-repeat='x in ar | filter: "1": false'>{{x}}</div>
等效于以下表达式:
<div ng-repeat='x in ar | filter: {$:1}: false'>{{x}}</div>
因此,不是简单的子字符串匹配,而是将对象与特殊键(默认为$
)一起使用,以将对象的任何属性与值1
进行匹配.
So instead of a simple substring match, an object is used with a special key ($
by default) to match any property of the object with the value 1
.
anyPropertyKey
参数可用于覆盖默认的$
属性,如下所示:
The anyPropertyKey
parameter can be used to overwrite the default $
property like this:
<div ng-repeat='x in ar | filter: {"@":1}: false: "@"'>{{x}}</div>
在最后一个示例中,我们使用@
符号来匹配任何属性名称,并释放$
用于其他目的.
In this last example we use the @
symbol to match any property name, freeing up the $
for other purposes.
以下代码段显示了所有三个表达式的相同输出:
The following snippet shows the same output for all three expressions:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<body ng-app="">
<div ng-init="ar=[{n:'ram1', m:1}, {n:'mah', m:1}, {n:'vij', m:3}]">
<div ng-repeat='x in ar | filter: "1": false'>{{x}}</div>
<hr />
<div ng-repeat='x in ar | filter: {$:1}: false'>{{x}}</div>
<hr />
<div ng-repeat='x in ar | filter: {"@":1}: false: "@"'>{{x}}</div>
<hr />
</div>
</body>
这篇关于Angular过滤器定义中的"anyPropertyKey"是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!