Angularjs 如何使指令使用指令属性中指定的控制器?
所以我有一个指示:Angularjs 如何使指令使用指令属性中指定的控制器?,angularjs,angularjs-directive,angularjs-controller,Angularjs,Angularjs Directive,Angularjs Controller,所以我有一个指示: <directive data="user" templateUrl="./user.html" controller="UserController"></directive> 您可以执行以下操作(不完全是您要求的-它会创建一系列嵌套作用域,但应该足够了): .directive('directive',()=>{ 作用域:{“数据”:“=”}, 模板:(元素,属性)=>{ 返回“”; }
<directive data="user" templateUrl="./user.html" controller="UserController"></directive>
您可以执行以下操作(不完全是您要求的-它会创建一系列嵌套作用域,但应该足够了):
.directive('directive',()=>{
作用域:{“数据”:“=”},
模板:(元素,属性)=>{
返回“”;
}
});
- 您可以直接使用$templateCache而不是ng include
- 如果您需要控制器/模板/。。。要动态,您需要观察/观察+dom操作+重新编译内容
.directive('directive',()=>{
作用域:{“数据”:“=”},
模板:(元素,属性)=>{
返回“”;
}
});
- 您可以直接使用$templateCache而不是ng include
- 如果您需要控制器/模板/。。。要动态,您需要观察/观察+dom操作+重新编译内容
app.directive('directive', function() {
return {
scope: { 'data' : '=' },
link: function (scope) {
// this makes your fields available as {{name}} instead of {{user.name}}:
Object.assign(scope, scope.data);
},
template: function(element, attrs) {
var controllerName = attrs.controller;
var controllerString = controllerName + ' as vm';
// check if controller extists:
var services = [];
app['_invokeQueue'].forEach(function(value){
services[value[2][0]] = true;
})
if (!services[controllerName]) {
return '<div ng-include="\'' + attrs.templateurl + '\'"></div>';
} else {
return '<div ng-controller="' + controllerString + '"><div ng-include="\'' + attrs.templateurl + '\'"></div></div>';
}
}
}
});
app.directive('directive',function(){
返回{
作用域:{“数据”:“=”},
链接:功能(范围){
//这使您的字段可用作{{name}而不是{{user.name}:
分配对象(范围、范围、数据);
},
模板:函数(元素、属性){
var控制器名称=属性控制器;
var controllerString=controllerName+‘作为vm’;
//检查控制器是否存在:
var服务=[];
app[''u invokeQueue'].forEach(函数(值){
服务[值[2][0]]=true;
})
如果(!服务[controllerName]){
返回“”;
}否则{
返回“”;
}
}
}
});
好的,在分析了Petr的答案后,我使用嵌套div发布了工作代码:
app.directive('directive', function() {
return {
scope: { 'data' : '=' },
link: function (scope) {
// this makes your fields available as {{name}} instead of {{user.name}}:
Object.assign(scope, scope.data);
},
template: function(element, attrs) {
var controllerName = attrs.controller;
var controllerString = controllerName + ' as vm';
// check if controller extists:
var services = [];
app['_invokeQueue'].forEach(function(value){
services[value[2][0]] = true;
})
if (!services[controllerName]) {
return '<div ng-include="\'' + attrs.templateurl + '\'"></div>';
} else {
return '<div ng-controller="' + controllerString + '"><div ng-include="\'' + attrs.templateurl + '\'"></div></div>';
}
}
}
});
app.directive('directive',function(){
返回{
作用域:{“数据”:“=”},
链接:功能(范围){
//这使您的字段可用作{{name}而不是{{user.name}:
分配对象(范围、范围、数据);
},
模板:函数(元素、属性){
var控制器名称=属性控制器;
var controllerString=controllerName+‘作为vm’;
//检查控制器是否存在:
var服务=[];
app[''u invokeQueue'].forEach(函数(值){
服务[值[2][0]]=true;
})
如果(!服务[controllerName]){
返回“”;
}否则{
返回“”;
}
}
}
});
app.directive('directive', function() {
return {
scope: { 'data' : '=' },
link: function (scope) {
// this makes your fields available as {{name}} instead of {{user.name}}:
Object.assign(scope, scope.data);
},
template: function(element, attrs) {
var controllerName = attrs.controller;
var controllerString = controllerName + ' as vm';
// check if controller extists:
var services = [];
app['_invokeQueue'].forEach(function(value){
services[value[2][0]] = true;
})
if (!services[controllerName]) {
return '<div ng-include="\'' + attrs.templateurl + '\'"></div>';
} else {
return '<div ng-controller="' + controllerString + '"><div ng-include="\'' + attrs.templateurl + '\'"></div></div>';
}
}
}
});