nevero nevero - 1 year ago 127
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'">

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


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 Source

Sure you can. Something like:


<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 ng-if="key == 'Town'">
             <pre>Town!!! {{$parent.localityTypeRadio}}</pre>


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download