gpt4 book ai didi

javascript - 有没有办法从 AngularJS 指令中获取外部 Controller 名称?

转载 作者:行者123 更新时间:2023-11-28 08:31:34 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com