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

3z6pesqy  于 2023-03-28  发布在  Angular
关注(0)|答案(5)|浏览(148)

这是怎么回事?
以下是我的指令:

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
0kjbasz6

0kjbasz61#

如下所述:Angular NgModelController,您应该为<input提供所需的控制器ngModel

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

iecba09b2#

一个可能的解决方案是ng-model属性需要使用该指令。
因此,添加“ng-model”属性可以解决这个问题。

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

rqcrx0a63#

您也可以删除线

require: 'ngModel',

删除ngModel将允许您创建没有ngModel错误指令。

yjghlzjz

yjghlzjz4#

使用ng-value而不是ng-model也会导致问题
错误:

<input ng-value="item.name" ng-change="nameChanged()">

作品:

<input ng-model="item.name" ng-change="nameChanged()">
cgh8pdjw

cgh8pdjw5#

我遇到了同样的错误,在我的情况下,我错过了拼写ng-model指令类似于“ng-moel”
错误:**ng-moel=”user.name“**正确:ng-model=“user.name”

相关问题