javascript 如何读取分配给 AngularJS 指令的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13160434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you read the value assigned to an AngularJS directive
提问by Dustin
I have an angular directive that is a custom attribute that possibly can contain a value like so:
我有一个 angular 指令,它是一个自定义属性,可能包含如下值:
<div my-directive="myVal"></div>
How do I read myVal (as a string) from inside my directive's link function?
如何从指令的链接函数中读取 myVal(作为字符串)?
回答by Ben Lesh
The passed value is on the attributes object passed as the third argument to the link function. It's under a property matching the name of the directive.
传递的值位于作为第三个参数传递给链接函数的属性对象上。它位于与指令名称匹配的属性下。
app.directive('myDirective', function() {
return {
restrict: 'A',
link: function(scope, elem, attr) {
//read the passed value
alert(attr.myDirective);
}
}
});