web-dev-qa-db-ja.com

ng-ifと変数でフィルターを使用するには?

この例では、 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}
];
26
Edward Tanguay

これを試してもらえますか?

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

前の回答を更新するために、3回フィルターする必要はありません。最初のフィルターで新しい変数「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>

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

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

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

これは私のために働く

2
SSD