[英]Getting HTML element class from AngularJS Directive
I have block of AngularJS code that looks like the following: 我有如下所示的AngularJS代码块:
<ion-list can-swipe="true">
<ion-item ng-repeat="item in items" simplify-item>
<div class="row" style="background-color:orange;">
Hello
</div>
</ion-item>
</ion-list>
The directives come from the ionic framework. 指令来自离子框架。 Still, I am trying to add my own directive to this.
不过,我仍在尝试为此添加自己的指令。 I want to set a CSS property via an attribute called "simplify-item".
我想通过一个名为“ simplify-item”的属性来设置CSS属性。 My directive to make this happen looks like this:
我的实现此目的的指令如下所示:
myApp.directive('simplifyItem', function() {
return {
restrict:'A',
link: function(element) {
console.log('linking element');
if (element === null) {
console.log('element is null');
} else {
var result = element.hasClass('item-complex');
if (result === null) {
console.log('result is null.');
} else {
console.log('Sweet!');
}
}
}
};
});
When the line var result = element.hasClass('item-complex');
当行
var result = element.hasClass('item-complex');
gets executed, I get an exception. 被执行,我得到一个例外。 The exception says:
异常说明:
undefined is not a function
I don't understand why I can't do this. 我不明白为什么我不能这样做。 What am I doing wrong?
我究竟做错了什么?
try like this 这样尝试
myApp.directive('simplifyItem', function() {
return {
restrict:'A',
link: function(scope , iElement, iAttrs) {
var element = iElement;
console.log('linking element');
if (element === null) {
console.log('element is null');
} else {
var result = element.hasClass('item-complex');
if (result === null) {
console.log('result is null.');
} else {
console.log('Sweet!');
}
}
}
};
});
explanation from angular docs 角度文档的解释
Directives that want to modify the DOM typically use the link option. 想要修改DOM的指令通常使用link选项。 link takes a function with the following signature, function link(scope, element, attrs) { ... } where:
link采用具有以下签名的函数,函数link(scope,element,attrs){...}其中:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.