161

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' || 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.

1
  • 3
    In JavaScript, the or operator is ||. Commented Nov 30, 2013 at 20:05

5 Answers 5

208

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

Sign up to request clarification or add additional context in comments.

2 Comments

If I have 20 input fields and I have to enable submit button only if all fields are filled. In this case, I have to include so many conditions. This is little bit confusing. Is there any other feasible solution?
@CharanCherry Take a look into angular form validation. That way you can set all desired fields ng-required and check for form validity in for example an ng-disabled expression on the submit button. cfr fdietz.github.io/recipes-with-angular-js/using-forms/…
80

OR operator:

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

Even though it is simple enough to read, I hope as a developer you are use better names than 'a' 'k' 'b' etc..

For Example:

<div class="links-group" ng-repeat="group in groups" ng-show="!group.hidden">
    <li ng-if="user.groups.admin || group.title == 'Home Pages'"> 
        <!--Content-->
    </li>
</div>

Another OR example

<p ng-if="group.title != 'Dispatcher News' or group.title != 'Coordinator News'" style="padding: 5px;">No links in group.</p>

AND operator (For those stumbling across this stackoverflow answer looking for an AND instead of OR condition)

<div class="links-group" ng-repeat="group in groups" ng-show="!group.hidden">
    <li ng-if="user.groups.admin && group.title == 'Home Pages'"> 
        <!--Content-->
    </li>
</div>

4 Comments

Ok, so all browsers seem to recognize the & as a valid character. But it should be mentioned that using & in an attribute is not valid html. See <html.spec.whatwg.org/multipage/syntax.html#syntax-attributes>, more specifically: "Attribute values are a mixture of text and character references, except with the additional restriction that the text cannot contain an ambiguous ampersand." See also this answer: <stackoverflow.com/a/5320217/788553>.
If we must use compound statements in html to make angular work I would prefer to put the compound statements into a function attached to the scope, thus removing that logic away from the html section.
HTML5 was not written with Angular in mind. Instead the brilliant guys at google wrote Angular to instead - take advantage of the spec and thus writing an Angular Expression with "&" is very normal. Sure, I prefer to not put business rules in the View, just like when I do Razor View pages in asp.net MVC , but my answer is completely within the context of the question being asked.
There is nothing wrong with your answer Tom, I also write compound statements like the ones you show because I'm lazy (or in a hurry), and it works. But then I always remember this rule about escaping &. This is the reason I stumbled upon your answer. I just wanted to put those links there to make people aware of the HTML spec, like you said, "HTML5 was not written with Angular in mind".
1

you can also try with && for mandatory constion if both condtion are true than work

//div ng-repeat="(k,v) in items"

<div ng-if="(k == 'a' &&  k == 'b')">
    <!-- SOME CONTENT -->
</div>

Comments

0

JavaScript Code

function ctrl($scope){
$scope.call={state:['second','first','nothing','Never', 'Gonna', 'Give', 'You', 'Up']}


$scope.whatClassIsIt= function(someValue){
     if(someValue=="first")
            return "ClassA"
     else if(someValue=="second")
         return "ClassB";
    else
         return "ClassC";
}
}

Comments

0

HTML code

<div ng-app>
<div ng-controller='ctrl'>
    <div ng-class='whatClassIsIt(call.state[0])'>{{call.state[0]}}</div>
    <div ng-class='whatClassIsIt(call.state[1])'>{{call.state[1]}}</div>
    <div ng-class='whatClassIsIt(call.state[2])'>{{call.state[2]}}</div>
    <div ng-class='whatClassIsIt(call.state[3])'>{{call.state[3]}}</div>
    <div ng-class='whatClassIsIt(call.state[4])'>{{call.state[4]}}</div>
    <div ng-class='whatClassIsIt(call.state[5])'>{{call.state[5]}}</div>
    <div ng-class='whatClassIsIt(call.state[6])'>{{call.state[6]}}</div>
    <div ng-class='whatClassIsIt(call.state[7])'>{{call.state[7]}}</div>
</div>

JavaScript Code

function ctrl($scope){
$scope.call={state:['second','first','nothing','Never', 'Gonna', 'Give', 'You', 'Up']}


$scope.whatClassIsIt= function(someValue){
     if(someValue=="first")
            return "ClassA"
     else if(someValue=="second")
         return "ClassB";
    else
         return "ClassC";
}
}

1 Comment

Could you please combine your answers into one and delete the other one?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.