我正在尝试根据其类有条件地将指令应用于元素。
这是我的问题的一个简单案例,请参阅此 fiddle 中的结果。对于这个例子,我使用类名映射到布尔形式ng-class
with 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>
JS:
angular.module('example', [])
.directive('testcase', function() {
return {
restrict: 'C',
link: function(scope, element, attrs) {
element.css('color', 'red');
}
}
}
);
为什么该指令不应用于div
通过它的类ng-class
?我是否误解了 AngularJS 处理指令的顺序?
我应该如何根据表达式的评估有条件地将指令应用于元素?