[英]Cannot select ngClass element from directive
Im using angularjs
ng-class
directive
to add class with ng-if
, and Im using another modules directive to do something with this element with this class name, but on runtime it's not working directive cannot find the element with this class name, when i hard code the class name its working, but from ng-class injected class name not working. 我使用
angularjs
ng-class
directive
添加类与ng-if
,并使用另一个模块指令使用此类名称对此元素执行某些操作,但在运行时它不工作指令找不到具有此类名的元素,当我硬编码类名称其工作,但从ng-class注入的类名不起作用。
<!-- 'A' not picking by directive -->
<div ng-class="{'myclass': item.isTrue}" ng-repeat="item in vm.list">...</div>
<-- 'B' working -->
<div class="myclass" ng-repeat="item in vm.list">...</div>
Directive. 指示。
var tmp = $element[0].getElementsByClassName("myclass"); // not working with 'A' but fine with 'B'
I guess there's an issue with the ng-class
of the directive
call. 我猜这个
directive
调用的ng-class
存在问题。 Also, the directive of type class
is used less often as one can always the achieve the same with that of the attribute
and element
. 此外,类型
class
的指令使用较少,因为总是可以实现与attribute
和element
相同的指令。
I have created a demo , how one can achieve using the same using ng-if
my invoking the directive
of type attribute
and class
: 我创建了一个演示 ,
ng-if
我调用type attribute
和class
的directive
,如何使用相同的方法实现:
- Using it as Attribute
将其用作属性
<div ng-repeat="item in data">
<div>
{{item.name}}
<span ng-if="item.isTrue" myclass></span>
</div>
</div>
- Using it as Class
用它作为类
<div ng-repeat="item in data">
<div>
{{item.name}}
<span ng-if="item.isTrue" class="myclass">
</span>
</div>
</div>
The JS for both of them is common as below: 它们的JS很常见,如下所示:
$scope.data = [{
'id': 0,
'name': 'Name 1',
'isTrue': false
}, {
'id': 1,
'name': 'Name 2',
'isTrue': true
}, {
'id': 2,
'name': 'Name 3',
'isTrue': true
}, {
'id': 3,
'name': 'Name 4',
'isTrue': false
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.