【发布时间】:2021-02-10 17:58:32
【问题描述】:
所以我有一个指令:
<directive data="user" templateUrl="./user.html" controller="UserController"></directive>
我希望该指令使用“控制器”属性中指定的控制器,如上所示。
AngularJS 指令是否可行?或者我应该用其他方式,也许用组件?
我的代码目前如下所示:
app.directive('directive', function() {
var controllerName = "UserController"; // i want that to dynamicaly come from attribute
// check if controller extists:
var services = [];
app['_invokeQueue'].forEach(function(value){
services[value[2][0]] = true;
});
if (!services[controllerName]) controllerName = false;
return {
scope: { 'data' : '=' },
link: function (scope) {
Object.assign(scope, scope.data);
},
templateUrl: function(element, attr) {
return attr.templateurl;
},
controller: controllerName
}
});
【问题讨论】:
标签: angularjs angularjs-directive angularjs-controller