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

标签 javascript angularjs angularjs-directive

我有这个指令:

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/

相关文章:

php - 将 PHP 添加到外部 JAVASCRIPT 文件

javascript - 使用 JavaScript 检查对象的可见性

JavaScript if else 问题

javascript - angularjs 指令不起作用(奇怪的问题)

angularjs - 创建 AngularJS 指令时遇到问题

javascript - TypeError : PF(. ..).hide 不是函数

javascript - 如果没有数据将显示消息

java - 将 Spring MVC 应用程序从 JSP 迁移到 AngularJS

javascript - Angular1.3 : Access controller function inside recursive directive template?

javascript - $http.get 从成功调用错误函数