缺少 Angular JS 必需控制器错误:找不到指令所需的控制器“ngModel”

Angular JS Missing Required Controller error: Controller 'ngModel', required by directive can't be found

本文关键字:控制器 ngModel 指令 JS Angular 错误 缺少 找不到      更新时间:2023-09-26

我有这个指令:

directive('myDirective',
    function() {
        return {
            restrict: 'EA',
            scope: {
                params: '=ngModel'
            },
            //template: '',
            templateUrl: '/myTemplate.html',
            controller: 'myController',
            link: function(scope, iElement, iAttrs, ngModel) {
               // code.. 
            }
        };
    }
);

但是,当我使用此指令时,我在控制台中收到以下错误:$compile:ctreq,带有指向以下消息的超链接:缺少所需的控制器组件$compile错误找不到指令"myDirective"所需的控制器"ngModel"!

如果我使用"模板"而不是"模板

URL",错误就会消失,并且我不想使用"模板"。这似乎是一个已知问题:https://github.com/angular/angular.js/issues/4603

任何人都可以建议解决方法吗?编辑:我正在使用ngModel,因为我想要2路绑定

如果你想将参数传递给指令,你可以这样做 - 你的问题对我来说看起来不太对。

<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...
            }
       };
   }

);