ui-date
ui-date copied to clipboard
ngModel don't updated in controller
I'm using ui-date with ng-model, and in the view the value is changed but in the controller remains undefined or with the value I use to initialize it:
Controller:
$scope.day = new Date;
$scope.dateOptions = { minDate: '0', dateFormat: 'yy-mm-dd' };
$scope.checkSchedule = function () {
console.log($scope.day);
};
View:
When I change the selected value, it's reflected in view (I have {{day}} after the datepiccker to see it). But when I submit the form, the value it's in today (because I inicialized as a new Date). And if I remove the inicilization, the value in the submit is undefined
Hello @sebrojas14. I'm just doing research on migrating from AngularJS 1.2 to 1.3. This sounds like it could be the reason.
https://code.angularjs.org/1.3.14/docs/guide/migration
$setViewValue(value) - This method still changes the $viewValue but does not immediately commit this change through to the $modelValue as it did previously. Now the value is committed only when a trigger specified in an associated ngModelOptions directive occurs. If ngModelOptions also has a debounce delay specified for the trigger then the change will also be debounced before being committed. In most cases this should not have a significant impact on how NgModelController is used: If updateOn includes default then $setViewValue will trigger a (potentially debounced) commit immediately.
code from the uiDate directive:
// Override ngModelController's $setViewValue
// so that we can ensure that a Date object is being pass down the $parsers
// This is to handle the case where the user types directly into the input box
var _$setViewValue = controller.$setViewValue;
var settingValue = false;
controller.$setViewValue = function () {
if ( !settingValue ) {
settingValue = true;
element.datepicker("setDate", element.datepicker("getDate"));
_$setViewValue.call(controller, element.datepicker("getDate"));
$timeout(function() {settingValue = false;});
}
};
I'm having the same problem. I would like to set a default in the controller and it is not working.
Having same issue. only applies when the 'dateFormat' options is specified.
I could be misunderstanding the problem, but the code in https://github.com/alexanderchan/ui-date/blob/issue-dateformat/demo/index.html appears to work with the recent beta release and up to date angular libraries.
Same here and kinda fucked up my forms :(
Same problem here. I am using format of YYYY-MM-DD, which works fine, but model doesn't get updated.
Hopefully this is helpful because I was having the same issue. I've also had this issue with angularjs' select when using ng-options. If you change
$scope.day
to
$scope.date.day
the ngModel should recognize and update accordingly. At least it has worked for me.
I believe this is an issue related to a scope issue. Having a $scope.vm
and storing the data in $scope.vm.date
solves this issue as same as @bkelsey stated.
I tried setting unique "id" for each date picker instances. And it worked.
<input ui-date ui-date-format = "yy-mm-dd" ng-model = "dateModel" id = "{{datepickerDynamicID}}">
Here the dateModel is inside the isolated scope of a directive.