对于日期选择AngularJS定制指令不更新NG-模型 [英] AngularJS custom directive for datepicker do not update ng-model
问题描述
我查了类似主题的所有悬而未决的问题,但没有给我这我逼疯了适当的工作解决方案,这就是为什么我打开了这个问题。
I checked all open questions with similar topic but neither gives me proper working solution which drives me crazy and that is why I opening this question.
我的自定义指令看起来像
My custom directive looks like
angular.module('angularSampleApp') .directive('datepicker', function() {
'use strict';
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, el, attr, ngModel) {
$(el).datepicker({
maxDate: 0 ,
onSelect: function(dateText) {
scope.$apply(function(){
ngModel.$setViewValue(dateText);
ngModel.$viewValue = dateText;
ngModel.$render();
});
}
});
}
};
});
我用指令像这样
<input type="text" id="datepicker" datepicker ng-model="jumpDate">
<a name="jump" id="jump" title="Jump" ng-click="jumpToDate()">Jump</a>
和控制器中我有这样的事情:
and in controller I have something like this:
$scope.jumpToDate = function() {
console.log($scope.jumpDate);
};
不过指令不更新$ scope.jumpDate它总是不确定的,即使我设置默认值控制它从来没有更新,dateText有适当的值。
However directive do not update $scope.jumpDate and it's always undefined even if I set default value in controller it's never updated, dateText have proper value.
我使用的角度构建1.4.1
I'm using angular build 1.4.1
推荐答案
我要检查指令的范围值。我猜测,NG-模式设置在隔离范围,而不是控制器中的值。
I would check the scope values on the directive. I'm guessing that ng-model is setting the value on the isolate scope, not the controller.
,或者尝试使用
ng-model="$parent.jumpDate"
这篇关于对于日期选择AngularJS定制指令不更新NG-模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!