I have a directive:
我有一个指令:
app.directive('testDir', [function () {
return {
require: '?ngModel',
link: function ($scope, elm, attr, ngModel) {
var abc=attr.testDir;
var def=< The value zzz >
}
};
}])
I understand I can call this like:
我明白我可以称之为:
<div test-dir='abcd'>xx</div>
What if I also needed to pass the parameter 'zzz'. How can I pass more than one parameter to my directive?
如果我还需要传递参数'zzz'怎么办?如何将多个参数传递给我的指令?
2 个解决方案
#1
2
Use multiple attributes. Your directive has access to all attributes used in the element:
使用多个属性。您的指令可以访问元素中使用的所有属性:
<div my-directive arg-one='abcd', arg-two>xx</div>
app.directive('myDirective', function () {
return {
link: function ($scope, elm, attr, ngModel) {
var abc=attr.argOne;
var def=attr.argTwo;
}
};
});
Notice the change from -
to camelCase. This is done by AngularJS.
注意从 - 到camelCase的变化。这是由AngularJS完成的。
#2
0
You can use an array like so
您可以使用这样的数组
<div test-dir='["abcd","zzz"]'>xx</div>
Then in your directive you can do
然后在你的指令中你可以做到
var abc = JSON.parse(attr.testDir); // = Array ["abcd","zzz"]
#1
2
Use multiple attributes. Your directive has access to all attributes used in the element:
使用多个属性。您的指令可以访问元素中使用的所有属性:
<div my-directive arg-one='abcd', arg-two>xx</div>
app.directive('myDirective', function () {
return {
link: function ($scope, elm, attr, ngModel) {
var abc=attr.argOne;
var def=attr.argTwo;
}
};
});
Notice the change from -
to camelCase. This is done by AngularJS.
注意从 - 到camelCase的变化。这是由AngularJS完成的。
#2
0
You can use an array like so
您可以使用这样的数组
<div test-dir='["abcd","zzz"]'>xx</div>
Then in your directive you can do
然后在你的指令中你可以做到
var abc = JSON.parse(attr.testDir); // = Array ["abcd","zzz"]