我试图有条件地将指令应用于基于其类的元素.
这是我的问题的一个简单案例,请看这个小提琴的结果.对于这个例子,我使用类名映射到booleans形式的ng-classwith true; 在我的实际情况中,我想使用函数的布尔结果.
标记:
<div ng-app="example">
<div class="testcase">
This will have the directive applied as I expect
</div>
<div ng-class="{'testcase':true}">
This will not have the directive applied but I expect it to
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
JS:
angular.module('example', [])
.directive('testcase', function() {
return {
restrict: 'C',
link: function(scope, element, attrs) {
element.css('color', 'red');
}
}
}
);
Run Code Online (Sandbox Code Playgroud)
为什么指令不适用于div通过它的类ng-class?我是否误解了AngularJS处理指令的顺序?
我应该如何基于对表达式的求值有条件地将指令应用于元素?