[英]Pass variable to custom Angular directive
I have an accordion list, in which I want to be able to open certain items. 我有一个手风琴清单,我希望能够在其中打开某些物品。 I have created accordion-open directive, which should open the particular item if shouldBeOpen is true
我创建了手风琴打开指令,如果shouldBeOpen为true,则该指令应打开特定项目
Here is the HTML element (items are printed in loop) 这是HTML元素(项目以循环方式打印)
<div class="m-list-item-head accordion-item" accordion-open="{{shouldBeOpen}}">
And here is my custom directive 这是我的自定义指令
(function () {
'use strict';
angular.module('myApp').directive('accordionOpen', function () {
return {
restrict: 'A',
link: function(scope, element) {
if (open) {
$(element).closest('.accordion-item').siblings().slideDown('fast');
$(element).closest('.accordion-item').addClass('opened');
}
}
}
});
})();
I am not able to read the contents of accordion-open within the directive. 我无法阅读指令中的手风琴打开的内容。
The interpolate expressions are not required. 不需要插值表达式。 So change to :
因此更改为:
accordion-open="shouldBeOpen"
Next, add attrs
to your link function: 接下来,将
attrs
添加到链接函数中:
link: function(scope, element, attrs)
And use $eval
to get the value of the attribute in your link function: 并使用
$eval
获取链接函数中属性的值:
console.log(scope.$eval(attrs.accordionOpen))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.