0

I was expecting ng-class to at least do what a simple class does i.e. apply a css property. In the plunker example, if the replace ng-class with class, it works and I get the indent. i.e. ng-class="indentLeft" doesn't work but class="indentLeft" does. What am I missing ?

See a simple plunker here: http://plnkr.co/edit/moLu0BmgEYVm3xFQDcw8?p=preview

2
  • If class is what you need, then why do you need ng-class Commented Apr 19, 2014 at 4:44
  • I actually need to do further complicated stuff with ng-class, but if I'm stumbling at the first step, it is not very pleasant. I've reduced my problem to this simple question. Commented Apr 19, 2014 at 4:52

1 Answer 1

3

ngClass requires an expression to evaluate.

ng-class="{'indentLeft' : (item == true)}"

The json structure is property name in '' is the class you want to apply and that property value detracted if it should be applied via true/false

String Expression

When you do this, ng-class="indentLeft" angularjs doesn't know that it is a string and most likely is trying to evaluate it from the scope.

If you wrap the property in '' and make it a string literal the plunker should work correctly

ng-class="\'indentLeft\'"

Updated plnkr

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

2 Comments

Pls. elaborate why you think it needs an expression. The documentation says that one of the 3 ways it operates is "If the expression evaluates to a string, the string should be one or more space-delimited class names." I mean, isn't ng-class="indentLeft" an adequate expression in itself ?
It is not since indentLeft most likely being treated as a unknown scope property. If you do 'indentLeft' now it is a string and angular works correctly

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.