gpt4 book ai didi

javascript - Angular JS 缺少指令所需的 Controller 错误 : Controller 'ngModel' , 找不到

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

我有这个指令:

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/

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