我有这个指令:
directive('myDirective',
function() {
return {
restrict: 'EA',
scope: {
params: '=ngModel'
},
//template: '',
templateUrl: '/myTemplate.html',
controller: 'myController',
link: function(scope, iElement, iAttrs, ngModel) {
// code..
}
};
}
);
但是,当我使用此指令时,我在控制台中收到以下错误: $compile:ctreq,带有指向以下消息的超链接: 缺少所需的 Controller 组件 $compile 中出现错误 找不到指令“myDirective”所需的 Controller “ngModel”!
如果我使用“template”而不是“templateUrl”,错误就会消失,并且我不想使用“template”。 这似乎是一个已知问题:https://github.com/angular/angular.js/issues/4603
有人可以建议一个解决方法吗? 编辑:我使用 ngModel 因为我想要 2 路绑定(bind)
最佳答案
如果您想将参数传递给指令,您可以这样做 - 您问题中的内容对我来说看起来不太正确。
<div my-directive my-param="foo"></div>
directive('myDirective',
function() {
return {
restrict: 'EA',
scope: {
myParam: '='
},
//template: '',
templateUrl: '/myTemplate.html',
controller: 'myController',
link: function(scope, iElement, iAttrs, ngModel) {
scope.myParam...
}
};
}
);
关于javascript - Angular JS 缺少指令所需的 Controller 错误 : Controller 'ngModel' , 找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20629778/