Angularjs ng class not working as expected with loading

This piece of code

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

      

received an 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 -

how alert-danger

alert-success

?

Chunks

+3


source to share


1 answer


You must specify the class name if it contains a dash:

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

      



This is because the ng class accepts a JavaScript object as its value, and the object keys (or any JavaScript identifiers) must not contain a dash.

+8


source







All Articles