angularjs Error: No controller: ngModel-Collection of common programming errors
I’ve been working with AngularJS for a few days now, and I’m declaring two directives which work if I don’t require anything. But I need the ngModel to use the $setViewValue method.
My directives look like this :
The directives are defined like :
var Application = Application || {};
;(function(window, document, undefined) {
"use strict";
var CorinaJSDialog = {};
Application.CorinaJSDialog = angular.module("CorinaJSDialog", ["ngSanitize"]);
Application.CorinaJSDialog.constant("CorinaJSAPI", {
document : {
load : "/corina/api/loaddocument",
save : "/corina/api/savedocument"
}
});
Application.CorinaJSDialog.directive("corina", ["$timeout", function($timeout){
var object = {
restrict : "E",
require : "ngModel",
transclude : true,
replace : true,
priority : 1,
templateUrl : "corina.html",
controller : function($scope) {},
link : function(scope, element, attrs, controller) { console.log(controller); }
};
return object;
}]);
Application.CorinaJSDialog.directive("properties", ["$timeout", function($timeout){
var object = {
restrict : "E",
require : "?corina",
transclude : true,
replace : true,
priority : 2,
terminal : true,
templateUrl : "properties.html",
controller : function($scope) {},
link : function(scope, element, attrs, controller) {
var loadXeditable = function() {
$(element).editable({
mode: "inline",
display: function(value) {
controller.$setViewValue(value);
scope.$apply();
$(this).html(value);
}
});
};
$timeout(function() {
loadXeditable();
}, 10);
}
};
return object;
}]);
})(window, document);
And the templates that are loaded :
corina.html :
properties.html :
{{property.Name}}
Each time I run the above I get this error :
Error: No controller: ngModel
at Error ()
at i (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:41:165)
at l (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:43:252)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:47:425
at https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:96:330
at h (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:78:207)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:78:440
at Object.e.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:89:272)
at Object.e.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:87:124)
at Object.e.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:89:431) angular.min.js:62
Also, if I require the directive like ^mydir, I get the same error, or simply like mydir. Only if I use ?mydir I get no error, but then I cannot use ngModule directive. I would appreciate some input on what is it that I’m doing wrong.
-
As I interpret it, you have two element directives,
corinaandproperties, wherepropertieswants the controller fromcorina. That will always fail because because they need to be on the same element forrequire: 'corina'to work. But I see now in your template thatpropertiesare inside ofcorina, so what you should do inpropertiesis this:require : "^?corina"instead. That tells Angular to look for the corina controller in parent elements as well.Requiring
ngModelincorinawon’t work though, sinceng-modelisn’t set on the corina element. Yourng-modelis set on an element inside thecorinareplacement template, so there’s no way for thecorinadirective to get that ngModelController. You can only ask for controllers from the same element or from parent elements, not from child elements. If you really needngModelControlleryou should add your own directive on the same element that hasng-modeland requirengModelControllerthere. That directive can require thecorinacontroller (since thecorinacontroller is on a parent element) and pass thengModelControllerto thecorinacontroller.But I’m not really sure why you want to use
ng-modelhere in the first place? You’ve placed it on an anchor element which won’t do anything. What is the effect that your after of havingng-modelon an anchor element?
Originally posted 2013-12-02 01:34:29.