我一直在使用AngularJS幾天,現在我聲明瞭兩個指令,如果我不需要任何東西的話就可以工作。但我需要ngModel
來使用$setViewValue
方法。angularjs錯誤:沒有控制器:ngModel
我的指示是這樣的:
<corina>
<properties data-ng-repeat="property in Properties"></properties>
</corina>
僞指令定義如下:
corina.html
:
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);
而且已加載的模板
<div>
<div data-ng-transclude></div>
</div>
properties.html
:
<div>
<div class="row">
<span class="span4">{{property.Name}}</span>
<div class="span8">
<a href="#" data-type="{{property.Type | lowercase}}" data-ng-model="property.Value" data-name="{{ property.Name }}" data-placeholder="{{ property.Value }}">
<span data-ng-bind-html="{{ property.Value.toString() }}"></span>
</a>
</div>
</div>
</div>
我每次運行上面我得到這個錯誤:
Error: No controller: ngModel
at Error (<anonymous>)
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) <div data-ng-transclude=""> angular.min.js:62
另外,如果我需要像的指令,我得到了同樣的錯誤,或者乾脆像mydir
。只有當我使用?mydir
時,我不會收到任何錯誤,但是我不能使用ngModule
指令。如果我做錯了,我將不勝感激。
我相信對於一個指令能夠'require:'ngModel'','ng-model =「...」'必須在與指令相同的元素上使用。 – 2013-02-26 21:14:43
好吧,但即使我在其他指令中使用它,我也有'ng-model',但我仍然會得到同樣的結果,正如我在帖子底部提到的,如果我需要:「^ mydir」或'require:「mydir」'我得到相同的錯誤,只使用'require:「?mydir」'工作。在我的代碼中必須有一些東西,因爲在另一個項目上,我有相同的邏輯工作正常 – Roland 2013-02-26 21:17:50