在此示例中,我在ng-repeat
中使用过滤器,但如何在变量和ng-if
中使用过滤器,如:
{{languages.length | filter: {available: true}}}
和
ng-if="languages.length == 0 | filter: {available: true}"
见Fiddle。
HTML
<div ng-controller="mainController">
<div>There are {{languages.length}} languages in total.</div>
<div>??? There are {{languages.length}} languages available.</div>
<div ng-if="languages.length == 0">??? Sorry, there are no languages available.</div>
<ol>
<li ng-repeat="language in languages | filter: {available: true}">{{language.name}}</li>
</ol>
</div>
AngularJS
$scope.languages = [
{id:1, name:"German", available: false},
{id:2, name:"English", available: true},
{id:3, name:"French", available: false},
{id:4, name:"Italian", available: true},
{id:5, name:"Spanish", available: false}
];
答案 0 :(得分:47)
你能试试吗?
<div ng-controller="mainController">
<div>There are {{languages.length}} languages in total.</div>
<div>There are {{(languages|filter:{available:true}).length}} languages available.</div>
<div ng-if="(languages|filter:{available:true}).length == 0">Sorry, there are no languages available.</div>
<ol>
<li ng-repeat="language in languages | filter: {available: true}">{{language.name}}</li>
</ol>
</div>
答案 1 :(得分:6)
只是为了更新以前的答案,不需要过滤三次,在第一个过滤器上创建新变量'filtered':
<div ng-controller="mainController">
<div>There are {{languages.length}} languages in total.</div>
<div>There are {{(filtered = (languages|filter:{available:true})).length}} languages available.</div>
<div ng-if="filtered.length == 0">Sorry, there are no languages available.</div>
<ol>
<li ng-repeat="language in filtered">{{language.name}}</li>
</ol>
filtered=[{{filtered}}]
</div>
答案 2 :(得分:2)
<div ng-if="(answerText|lowercase)=='true'">T</div>
<div ng-if="(answerText|lowercase)=='false'">F</div>
这对我有用