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,
corina
andproperties
, whereproperties
wants 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 thatproperties
are inside ofcorina
, so what you should do inproperties
is this:require : "^?corina"
instead. That tells Angular to look for the corina controller in parent elements as well.Requiring
ngModel
incorina
won’t work though, sinceng-model
isn’t set on the corina element. Yourng-model
is set on an element inside thecorina
replacement template, so there’s no way for thecorina
directive 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 needngModelController
you should add your own directive on the same element that hasng-model
and requirengModelController
there. That directive can require thecorina
controller (since thecorina
controller is on a parent element) and pass thengModelController
to thecorina
controller.But I’m not really sure why you want to use
ng-model
here 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-model
on an anchor element?