[英]Angular JS Custom Directive not Working
我正在嘗試在我的應用程序中實現指令。 這是下面的代碼。
console.log("Directive loaded");
angular.module("MyApp")
.directive("datepickerLocal", function($rootScope) {
console.log("datepickerLocal");
return {
restrict: 'E',
require: 'ngModel',
link: function(scope, elem, attr, ctrl) {
ctrl.$parsers.unshift(function(viewValue) {
var ngModelController = ctrls[0];
ngModelController.$parsers.push(function (viewValue) {
viewValue.setMinutes(viewValue.getMinutes() - viewValue.getTimezoneOffset());
return viewValue.toISOString().substring(0, 10);
});
});
}
};
}
);
這是HTML文本字段。
<input id="datepicker" type="text" data-ng-datepicker data-ng-options="datepickerOptions" ng-model="user.User.DateOfBirth" datepickerLocal>
我能夠看到第一條控制台消息,但是指令內的控制台消息卻沒有出現。
反饋將不勝感激。
2件事:
E
,說應該將其定義為元素,但您將其用作屬性。 更改restrict: "E"
更改為A
或EA
,或將其像這樣使用: datepickerLocal
錯誤,應為: datepicker-local
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.