4

This piece of code

    <div ng-class="{alert: true, alert-success: geocode.success, alert-danger: !geocode.success}">{{geocode.status}}</div>

got error

Syntax Error: Token '-' is at column {2} of the expression [{3}] starting at [{4}].

But if I update the code to

    <div ng-class="{alert: true, green: geocode.success, red: !geocode.success}">{{geocode.status}}</div>

it works.

ng-class doesn't like css name with - like alert-danger alert-success?

THanks

1
  • imo may would be good changing your post title adding the really issue like ng-class not working properly with bootstrap dashed class or similar Commented Nov 5, 2014 at 20:10

1 Answer 1

9

You must quote the class name if it contains dashes:

<div ng-class="{'alert': true, 'alert-success': geocode.success, 'alert-danger': !geocode.success}">{{geocode.status}}</div>

This is because ng-class takes JavaScript object as its value and the object's keys (or any JavaScript identifiers for that matter) must not contain dashes.

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

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.