angularjs : setting class attribute in a directive

2019-08-15 22:19发布

问题:

I'm trying to set the class attribute of my directive. That's how I do it:

the relevant code in the directive

template: '<input class="myClass" />'

scope:{
 myClass = '@'
}

and that's how I call it

<my-directive my-class="someClass" />

Unfortunately, nothing happens. No errors, but the class is just not set.

Anything I'm missing ?

回答1:

myClass is a scope variable in your new isolated scope. You have to write template: '<input class="{{myClass}}" /> or template: '<input ng-class="myClass" />