我已经使用 AngularJS 几天了,我声明了两个指令,如果我不需要任何东西,它们就可以工作。但我需要ngModel
使用该$setViewValue
方法。
我的指令如下所示:
<corina>
<properties data-ng-repeat="property in Properties"></properties>
</corina>
这些指令的定义如下:
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);
以及加载的模板:
corina.html
:
<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
. 只有当我使用?mydir
我没有得到错误,但我不能使用ngModule
指令。我会很感激一些关于我做错了什么的意见。