17

What is the difference between the following two code snippets,
Code 1:

<div class={{getClass()}}/>

Code 2:

<div ng-class=getClass()/>
4
  • 2
    I believe ng-class will be evaluated at the end of every digest, where as {{ getClass() }} is evaluated once. Commented Jul 4, 2014 at 17:01
  • ng-class is usually used to evaluate expressions and apply class(es) based on expressions Commented Jul 4, 2014 at 17:05
  • 2
    @Pete not really, since it's in an angular expression it will get evaluated with any scope change Commented Jul 4, 2014 at 17:08
  • 1
    @charlietfl Good point. Just ran a test and it does get evaluated. Commented Jul 4, 2014 at 17:09

1 Answer 1

7

With the first example before angular loads your class will literally be "{{getClass()}}". While in the second example the div won't have a class until angular runs its first digest.

There might be some minor differences with when they are recalculated but Angular will keep both up to date. I've run into issues before using the first method with Animation as ng-class has some hooks into animation.

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

3 Comments

they are not really the same when you get into using conditionals in ng-class. Outcome of OP example will result in same but ng-class has more specific use than class also
I mean that his two example are pretty much the same. Not ng-class and class="{{}}"
right but in a case like class="form-group" ng-class="{ 'has-error': form.course.subtitle.$invalid && isSubmitted}" they are not anywhere near the same

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.