Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

ngModel don't updated in controller #122

Open
quedicesebas opened this issue May 30, 2015 · 9 comments
Open

ngModel don't updated in controller #122

quedicesebas opened this issue May 30, 2015 · 9 comments

Comments

@quedicesebas
Copy link

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 quedicesebas changed the title ngModel don't updatet in controller ngModel don't updated in controller May 30, 2015
@wbeange
Copy link

wbeange commented Jun 1, 2015

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
Copy link

wbeange commented Jul 8, 2015

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

@BelfordZ
Copy link

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

@alexanderchan
Copy link
Contributor

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.

@mgohin
Copy link

mgohin commented Jan 18, 2016

Same here and kinda fucked up my forms :(

@gnbonney
Copy link

gnbonney commented Apr 1, 2016

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

@bkelsey
Copy link

bkelsey commented May 11, 2016

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.

@anonrig
Copy link

anonrig commented May 13, 2016

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.

@Enkuushka
Copy link

Enkuushka commented Dec 6, 2016

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants