找不到指令“ ...”所需的控制器“ ngModel”


88

这里发生了什么?

这是我的指令:

app.directive('submitRequired', function (objSvc) {
    return {
        require: 'ngModel',
        link: function (scope, elm, attrs, ctrl) {

          // do something
        }
    };
});

这是使用中的指令的示例:

<input submit-required="true"></input>

这是实际的错误文本:

Error: [$compile:ctreq] Controller 'ngModel', required by directive 'submitRequired', can't be found!
http://errors.angularjs.org/1.2.2/$compile/ctreq?p0=ngModel&p1=submitRequired
    at http://www.domain.ca/Scripts/angular/angular.js:78:12
    at getControllers (http://www.domain.ca/Scripts/angular/angular.js:5972:19)
    at nodeLinkFn (http://www.domain.ca/Scripts/angular/angular.js:6139:35)
    at compositeLinkFn (http://www.domain.ca/Scripts/angular/angular.js:5550:15)
    at nodeLinkFn (http://www.domain.ca/Scripts/angular/angular.js:6132:24)
    at compositeLinkFn (http://www.domain.ca/Scripts/angular/angular.js:5550:15)
    at publicLinkFn (http://www.domain.ca/Scripts/angular/angular.js:5458:30)
    at http://www.domain.ca/Scripts/angular/angular.js:1299:27
    at Scope.$get.Scope.$eval (http://www.domain.ca/Scripts/angular/angular.js:11634:28)
    at Scope.$get.Scope.$apply (http://www.domain.ca/Scripts/angular/angular.js:11734:23) <input submit-required="true"> angular.js:9159
(anonymous function) angular.js:9159
$get angular.js:6751
nodeLinkFn angular.js:6141
compositeLinkFn angular.js:5550
nodeLinkFn angular.js:6132
compositeLinkFn angular.js:5550
publicLinkFn angular.js:5458
(anonymous function) angular.js:1299
$get.Scope.$eval angular.js:11634
$get.Scope.$apply angular.js:11734
(anonymous function) angular.js:1297
invoke angular.js:3633
doBootstrap angular.js:1295
bootstrap angular.js:1309
angularInit angular.js:1258
(anonymous function) angular.js:20210
trigger angular.js:2315
(anonymous function) angular.js:2579
forEach angular.js:300
eventHandler angular.js:2578ar.js:7874

Answers:


127

如此处所述:Angular NgModelController,您应提供<input所需的控制器ngModel

<input submit-required="true" ng-model="user.Name"></input>

1
完善。我很感激!我将其标记为答案。我有一个后续问题。我应该发布其他问题还是更改原件?
Shaun Luttin 2014年


2
我输入ng-models错误并收到此错误。
chovy

@Radim Kohler我很高兴您的回复实际上帮助了需要帮助的人,我本人已接近实现这一目标。从上面的输入语句中,对于“ ng-model”属性,我可以使用字符串连接,例如“ {{RootObjectName +”。“ + ModelName}}” ??!正如我在$范围模型并不简单,基于输入从DB控制器是动态创建
帕万·库马尔·

@pavankumar检查此链接next.plnkr.co/edit/...ng-model="RootObject[alias]"会工作,如果会议将`$ scope.RootObject = {}; $ scope.alias =“姓”`...别名instaead,甚至的forEach可以提供动态域名
拉迪姆·克勒

8

解决此问题的一种可能方法ng-model是使用该指令需要属性。

因此,添加“ ng-model”属性可以解决该问题。

<input submit-required="true" ng-model="user.Name"></input>

这解决了我的问题。谢谢。我认为我们错过了触发ng-change的要点,应该将ng-model绑定到元素。
antonD

1

您也可以删除该行

  require: 'ngModel',

如果您不需要ngModel此指令。删除ngModel将使您可以做出没有该ngModel错误的指令。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.