Angular JS
{{ filter_expression | filter : expression : comparator : anyPropertyKey}}
我无法理解最后一个参数,即anyPropertyKey.任何人都可以用一个简单的例子来解释我
<div ng-init="ar=[{n:'ram1',m:1},{n:'mah',{n:'vij',m:3}]"> <div ng-repeat='x in ar | filter: "1": false: propertyName'>{{x}}</div> </div>
这是我到目前为止所尝试的.在上面的代码中我应该写的不是propertyName.
解决方法
anyPropertyKey与过滤器的表达式相关.
documentation的相关部分是:
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.
所以这里的第一件事是这个表达式:
<div ng-repeat='x in ar | filter: "1": false'>{{x}}</div>
相当于这个表达式:
<div ng-repeat='x in ar | filter: {$:1}: false'>{{x}}</div>
因此,对象与特殊键(默认情况下为$)一起使用,以匹配对象的任何属性,而不是简单的子字符串匹配.
anyPropertyKey参数可用于覆盖默认的$property,如下所示:
<div ng-repeat='x in ar | filter: {"@":1}: false: "@"'>{{x}}</div>
在最后一个例子中,我们使用@符号匹配任何属性名称,释放$用于其他目的.
<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: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>