gpt4 book ai didi

javascript - ng-model 未更新日期时间字段已更改

转载 作者:行者123 更新时间:2023-12-03 08:13:54 24 4
gpt4 key购买 nike

所以我在 AngularJS 上的日期选择器指令上遇到了问题。输入已分配指令以及 ng-model,但当有人选择日期时它不会更新模型。但是,如果选择日期后,您直接在输入字段上手动修改日期,它就会更新。

这是我的指令:

.directive('jqdatepicker', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
element.datepicker({
dateFormat: 'yy-mm-dd',
changeMonth: true,
changeYear: true,
onSelect: function (date) {
scope.date = date;
scope.$apply();
}
});
}
};
});

这是我的 HTML 输入:

<input type="datetime" class="form-control" ng-model="selectedUser.birthday" jqdatepicker />

注意:该字段位于从 Controller 调用的模态上,如下所示:

$modalInstance = $modal.open({
scope: $scope,
templateUrl: 'views/modal_user_form.html',
windowClass: "animated flipInY"
});

提前致谢。

最佳答案

只需使用 onClose 事件而不是 onSelect 即可,但我注意到您的代码中存在小错误 - 您应该使用 ngModelCtrl:

.directive('jqdatepicker', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
element.datepicker({
dateFormat: 'yy-mm-dd',
changeMonth: true,
changeYear: true,
onSelect: function (date) {
scope.$apply(function() {
ngModelCtrl.$setViewValue(date);
});
}
});
}
};
});

骗子:http://plnkr.co/edit/kU5g5ov1tduycjd8tUWN?p=preview

关于javascript - ng-model 未更新日期时间字段已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34032694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com