Javascript 如何在 ng-if 和变量中使用过滤器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31455567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:43:32  来源:igfitidea点击:

How to use filter in ng-if and variable?

javascriptangularjsangularjs-filterangular-ng-if

提问by Edward Tanguay

In this example, I use filter in the ng-repeat, but how do I use it in a variable and ng-if, something like:

在这个例子中,我在 中使用过滤器ng-repeat,但是我如何在变量和中使用它ng-if,例如:

{{languages.length | filter: {available: true}}}

and

ng-if="languages.length == 0 | filter: {available: true}"

See Fiddle.

小提琴



HTML

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

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

回答by Mistalis

Can you try this?

你能试试这个吗?

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

回答by Dmitri Algazin

Just to update previous answer, no need to filter three times, create new variable 'filtered' on first filter:

只是为了更新以前的答案,不需要过滤三次,在第一个过滤器上创建新变量“过滤”:

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

http://jsfiddle.net/to7z06ma/18/

http://jsfiddle.net/to7z06ma/18/

回答by SSD

<div ng-if="(answerText|lowercase)=='true'">T</div>

<div ng-if="(answerText|lowercase)=='false'">F</div>

<div ng-if="(answerText|lowercase)=='false'">F</div>

This works for me

这对我有用