无法找到指令'...'所需的控制器'ngModel'

时间:2014-02-16 05:49:23

标签: angularjs directive

这里发生了什么?

这是我的指示:

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

4 个答案:

答案 0 :(得分:123)

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

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

答案 1 :(得分:6)

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

因此添加了&#39; ng-model&#39;属性可以解决问题。

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

答案 2 :(得分:0)

您也可以删除行

# Given a data frame of patient data, I need to find records containing date logic errors.
# My datasets are enormous but here is a tiny example

patientData <- data.frame(
      patientID = 1:20,
      birth_d = seq(as.Date("2010-01-01"),by = 90, length.out = 20),
      treat_d = seq(as.Date("2011-01-01"),by = 90, length.out = 20),
      death_d = seq(as.Date("2012-01-01"),by = 90, length.out = 20)
 )

# To create some random records that will be in error (death_d before birth_d, birth_d after treat_d, etc):

patientData$birth_d[5] <- as.Date("2017-01-01")
patientData$death_d[7] <- as.Date("2001-01-01")
patientData$treat_d[10] <- as.Date("2018-01-01")
patientData$birth_d[12] <- as.Date("2018-05-05")

# To determine which records have birth_d after death_d I could do the following:

badRecords <- patientData %>% filter(death_d < birth_d)

OR

badRecords <- patientData %>% mutate(dateDiff = death_d - birth_d) %>% filter(dateDiff < 0)

# But in my large application (with lots and lots of date variables) 
# I want to be able to use the date field names as *variables* and, using one date pair at a time,
# determine which records have dates out of sequence. For example,

firstDateName <- "birth_d"
secondDateName <- "death_d"

# I would like to do this, but it doesn't work
badRecords <- patientData %>% filter(!!firstDateName > !!secondDateName)

# This doesn't work... 
badRecords <- patientData %>% mutate(dateDiff = !!secondDateName - !!firstDateName) %>% filter(dateDiff < 0)

# Neither does this... it creates a dateDiff data frame.. with 20 duplicate records
badRecords <- patientData %>% mutate(dateDiff = .[secondDateName] - .[firstDateName]) %>% filter(dateDiff < 0)

如果此指令中不需要 require: 'ngModel', 。删除ngModel可以使您做出的指令不会出现ngModel错误。

答案 3 :(得分:0)

我遇到了同样的错误,就我而言,我拼写了类似“ ng-moel”之类的ng-model指令

错误之一:   ng-moel =“ user.name” 正确的那个: ng-model =“ user.name”

enter image description here

相关问题