我在我的angularjs SPA项目中使用了http://mgcrea.github.io/angular-strap/##datepickers的angular-strap datepicker。当使用ngModel双向绑定编辑现有记录时,Angular不会检测日期字段的更改,即使我通过更新另一个非日期字段来强制保存表单,新的日期也不会在后端更新。下面是我的html文件中的相关部分:
<input name="DecisionDatePicker" id="ddpID" type="text" class="form-control input-medium" tabindex="14" placeholder="{{vm.format}}"
data-date-format="MM-dd-yyyy"
data-ng-model="vm.formData.dateDecision"
data-ng-required="vm.decisionDateRequired"
bs-datepicker />我在我的js文件中没有做任何特殊的事情。我正在使用breezejs来处理我的数据,它在其他领域工作得很好。我做错了什么?任何帮助都是非常感谢的。
发布于 2015-05-20 00:18:19
我也遇到过同样的问题,我用一个自定义指令解决了这个问题,当bs-datepicker调度一个blur事件时,它会更新模型:
angular.module('moduleName').directive('updateModelOnBlur',
['$parse', function($parse) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, elm, attrs) {
elm.bind("blur", function (event) {
scope.$apply(function () {
var model = $parse(attrs.ngModel);
model.assign( scope, elm.context.value );
});
});
}
};
}]
);我知道这只是一个技巧,并不是问题的最终解决方案。但是,如果你被卡住了,并想继续前进,有时一个技巧可能是有用的。
如果要在更新模型之前操作日期格式,还可以在指令中包含$filter服务。;)
https://stackoverflow.com/questions/21741645
复制相似问题