0

I came through the following example of ng-class="expression" in a book. http://plnkr.co/edit/fJGT5L9HZXvgAiAyXwlW?p=preview

ng-class="{error:isError,warning:isWarning}"

I get the controller logic, but my doubt is in the interpolation happening here.

What does the following scenarios mean (what is the evaluated value) and Why?

  1. ng-class="{error:true,warning:true}"
  2. ng-class="{error:true,warning:false}"
  3. ng-class="{error:false,warning:true}"
  4. ng-class="{error:false,warning:false}"
1
  • CSS classes will be set based on condition. If isError is true the error class will be added. And no interpolation is happening here. You are passing JSON TO ngClass Commented Oct 25, 2014 at 13:45

3 Answers 3

2

From the ngClass arguments docs:

The result of the arguments evaluation can be a string representing space delimited class names, an array, or a map of class names to boolean values. In the case of a map, the names of the properties whose values are truthy will be added as css classes to the element.

So in your case the result of the arguments after evaluation is a map which produces:

ng-class="{error:true,warning:true}"
=> class="error warning"

ng-class="{error:true,warning:false}"
=> class="error"

ng-class="{error:false,warning:true}"
=> class="warning"

ng-class="{error:false,warning:false}"
=> no class attribute
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Catalin. It helped me to understand.
2
  1. ng-class="{error:true,warning:true}" => class="error warning"
  2. ng-class="{error:true,warning:false}" => class="error"
  3. ng-class="{error:false,warning:true}" => class="warning"
  4. ng-class="{error:false,warning:false}" => no classes set

As you could notice ng-class get just hash where keys are names of classes and values -- conditions (if true than class will be added to class attribute)

So, ng-class just uses angular expression and check what data it returns (string, array or hash-map) then it applies certain parser to get final classes array to put it to class. One of this is described above.

Angular expressions

Angular like as Javascript uses eval mechanism to execute code (expression), but unlike JS Angular uses safe eval called $eval([expression], [locals]) (Docs)

I'm strongly recommending you to read this article about Angular expressions to understand how it works.

2 Comments

nice, but some explanation would probably help author of question :)
Thanks Max for the reply, and Jarema for getting my point :). I can see in the HTML but my doubt is how the object {error:true || false,warning: true || false} gets evaluated and gets assigned. Thanks
1

Angular will add (or append to the existing class attr) the following class attribute to the element:

  1. class="error warning"
  2. class="error"
  3. class="warning"
  4. No class attribute added

Comments

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.