nevero nevero - 24 days ago 23
AngularJS Question

AngularJS ng-if with multiple conditions

I'd like to know if it's possible to have something like this:

div ng-repeat="(k,v) in items"
<div ng-if="k == 'a' or k == 'b'">
<!-- SOME CONTENT -->
</div>


Knowing that items is a JSON container received through a request, so that's why I'm using a key, value method.

Thanks

I'm asking because I've tried googling it, but the only result I could get were with ng-switch, but I have to use ng-if.

Answer

Sure you can. Something like:

HTML

<div ng-controller="fessCntrl">    
     <label ng-repeat="(key,val) in list">
       <input type="radio" name="localityTypeRadio" ng-model="$parent.localityTypeRadio" ng-value="key" />{{key}}
         <div ng-if="key == 'City' || key == 'County'">
             <pre>City or County !!! {{$parent.localityTypeRadio}}</pre>
         </div>
         <div ng-if="key == 'Town'">
             <pre>Town!!! {{$parent.localityTypeRadio}}</pre>
         </div>        
      </label>
</div>

JS

var fessmodule = angular.module('myModule', []);

fessmodule.controller('fessCntrl', function ($scope) {

    $scope.list = {
        City: [{name: "cityA"}, {name: "cityB"}],
        County: [{ name: "countyA"}, {name: "countyB"}],
        Town: [{ name: "townA"}, {name: "townB"}]
      };

    $scope.localityTypeRadio = 'City';
});

fessmodule.$inject = ['$scope'];

Demo Fiddle

Comments