我是 AngularJS 的新手...这是问题:
例如:
HTML
<div ng-controller='DemoController'>
<div my-directive></div>
</div>
JavaScript
app.directive('myDirective', function(){
return {
restrict: 'A',
link: function(scope, element, attrs){
//I want to get the outer controller name (in this case 'DemoController') here when using this directive
// var controller_name = ?
}
};
});
如何获取外部 Controller 名称?
感谢您的帮助
最佳答案
您可以使用指令的属性将您想要的任何内容传递给指令。请参阅guide about directives 。在你的情况下,可以这样做:
HTML
<div ng-controller='DemoController'>
<div my-directive="DemoController"></div>
</div>
JS
app.directive('myDirective', function(){
return {
restrict: 'A',
link: function(scope, element, attrs){
var controller_name = attrs.myDirective;
}
};
});
关于javascript - 有没有办法从 AngularJS 指令中获取外部 Controller 名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21794979/