Angularjs filter not null

后端 未结 4 1560
执笔经年
执笔经年 2020-12-04 14:14

Trying to filter out items with a certain property that is not null So for:

var details = [{name:\'Bill\', shortDescription: null}, {name:\'Sally\', shortDes         


        
相关标签:
4条回答
  • 2020-12-04 14:28

    I think this is easier to read

     <ul>
        <li ng-repeat="detail in details" ng-if="detail.shortDescription">
            <p>{{detail.shortDescription}}</p>
        </li>
     </ul>
    
    0 讨论(0)
  • 2020-12-04 14:30

    Angular >=1.3.16 to latest (1.5.5 at time of writing/update) use '' (empty string) (or '!!' also works)

    <ul>
        <li ng-repeat="detail in details | filter:{shortDescription: ''}">
            <p>{{detail.shortDescription}}</p>
        </li>
    </ul>
    

    Example: http://jsfiddle.net/TheSharpieOne/1mnachk6/


    Angular >=1.3.6 and <=1.3.15 use '!null'

    <ul>
        <li ng-repeat="detail in details | filter:{shortDescription: '!null'}">
            <p>{{detail.shortDescription}}</p>
        </li>
    </ul>
    

    Example: http://jsfiddle.net/TheSharpieOne/4wxs67yv/


    Angular >=1.2 and <=1.3.5 use '' (empty string) (or '!!' also works)

    <ul>
        <li ng-repeat="detail in details | filter:{shortDescription: ''}">
            <p>{{detail.shortDescription}}</p>
        </li>
    </ul>
    

    Example: http://jsfiddle.net/TheSharpieOne/ovsqf17n/


    Angular <1.2 '!!'

    <ul>
        <li ng-repeat="detail in details | filter:{shortDescription: '!!'}">
            <p>{{detail.shortDescription}}</p>
        </li>
    </ul>
    

    Example: http://jsfiddle.net/TheSharpieOne/RGEdc/


    Overall, '!!' has the best support, but '' (empty string) is recommended and intended for this.

    0 讨论(0)
  • 2020-12-04 14:43

    According to https://github.com/angular/angular.js/issues/11573 for Angular >= 1.4, the recommendation is to use '' which matches any primitive except null/undefined.

    <ul>
        <li ng-repeat="detail in details | filter:{shortDescription: ''}">
            <p>{{detail.shortDescription}}</p>
        </li>
    </ul>
    
    0 讨论(0)
  • 2020-12-04 14:50

    It is worth noting that to use the empty quotes solution, you need to leave the comparator at its default of false. You maybe be used to putting true in here in your controller filters, like this :

    const myAssessments = 
           this.filterFilter(this.assessments, {userId: this.user.id}, true);
    

    That type of strict filter wouldn't work without the final true. But you need to drop the true or make it false for a not null check to work :

    const activeAndHistoric = 
          this.filterFilter(filteredAssessments, {historicYear: ''}, false);
    
    0 讨论(0)
提交回复
热议问题