ui-date icon indicating copy to clipboard operation
ui-date copied to clipboard

ngModel don't updated in controller

Open quedicesebas opened this issue 9 years ago • 9 comments

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:

{{day}}

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

quedicesebas avatar May 30 '15 23:05 quedicesebas

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;});
            }
          };

wbeange avatar Jun 01 '15 22:06 wbeange

I'm having the same problem. I would like to set a default in the controller and it is not working.

wbeange avatar Jul 08 '15 23:07 wbeange

Having same issue. only applies when the 'dateFormat' options is specified.

BelfordZ avatar Dec 15 '15 22:12 BelfordZ

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.

alexanderchan avatar Dec 19 '15 16:12 alexanderchan

Same here and kinda fucked up my forms :(

mgohin avatar Jan 18 '16 17:01 mgohin

Same problem here. I am using format of YYYY-MM-DD, which works fine, but model doesn't get updated.

gnbonney avatar Apr 01 '16 19:04 gnbonney

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.

bkelsey avatar May 11 '16 20:05 bkelsey

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.

anonrig avatar May 13 '16 12:05 anonrig

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.

Enkuushka avatar Dec 06 '16 22:12 Enkuushka