简体   繁体   中英

ng-if angular not working

My problem is that ng-if is not behaving as i pretend, i have an array of objects with _name attribute, im making a ng-repeat and inside that i want to discern between some that are named in specific way and the others. based on that im making my ng-if, but is printing two times the rows and it contents that should be either one or the other, anyone can point me out where im failing? tks

Below the array in clusterEntity.cluster;

       locations:{
          location:[
            {_name: "staging", _path: ""},
            {_name: "temp", _path: ""},
            {_name: "working", _path: ""}
          ]
        },

<div ng-repeat="location in clusterEntity.cluster.locations.location">

    <div ng-if="
                location._name === 'staging' || 
                location._name === 'working' || 
                location._name === 'temp'">
      <something here>
    </div>


    <div ng-if="
                location._name !== 'staging' || 
                location._name !== 'working' || 
                location._name !== 'temp'">

      <something there>
    </div>
  </div>

Your second ng-if should use logical AND, not OR:

<div class="row" ng-if="location._name !== 'staging' && location._name !== 'working' && location._name !== 'temp'">

which is the same as:

<div class="row" ng-if="!(location._name === 'staging' || location._name === 'working' || location._name === 'temp')">

You could use ng-switch instead, but your code wouldn't be much shorter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM