Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

$overrideModelOptions does not apply timezone #16181

Closed
booblin opened this issue Aug 18, 2017 · 2 comments · Fixed by #16336
Closed

$overrideModelOptions does not apply timezone #16181

booblin opened this issue Aug 18, 2017 · 2 comments · Fixed by #16336

Comments

@booblin
Copy link

booblin commented Aug 18, 2017

I'm thinking is bug

Current behavior:
Changing timezone programmatically (use $overrideModelOptions) dont work

Expected / new behavior:
If we set new timezone, it apply to model

Minimal reproduction of the problem with instructions:

function ChangeModelOptionsDirective () {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function ($scope, element, attrs, ngModel) {
      ngModel.$overrideModelOptions({
	timezone: '+0400', // dont work
	debounce: {
	  'default': 5000 // it work
        }
     })	     
    }
  };
}

Plunker: http://plnkr.co/edit/7zHR1EKigTMaFjDU1yDc?p=preview

Angular version - 1.6.5

@gkalpak
Copy link
Member

gkalpak commented Aug 18, 2017

You are right, it is a bug (or at least, I believe it is unintended behavior). The reason this happens is that in "date-family" directives, we cache the timezone in the directive's pre-linking phase, which comes before your $overrideModelOptions() call.

I think we shouldn't cache that value (or at least not until later).

In the meantime, you can ensure your call happens before caching the timezone, by moving it in the pre-linking phase and giving your directive a higher-than-default priority:

return {
  priority: 10,
  ...
  link: {
    pre: function ($scope, element, attrs, ngModel) {
      ngModel.$overrideModelOptions({...});
    }
  }
};

@booblin
Copy link
Author

booblin commented Aug 18, 2017

Yes, thank.
link pre override options when directive start, but for our business case we need change timezone (offset) in "online".

Now i use this solution

pre: (scope, element, attrs, ngModelCtrl) => {
  ngModelCtrl.$overrideModelOptions({
    timezone: 'utc'
  })
  ngModelCtrl.$parsers.push((value) => {
    let utc = moment.utc(value);
    let serverTimeZone = 'Europe/London'; // for example
    let tzOffsetMinutes = moment.tz(utc, serverTimeZone).utcOffset();
    let serverLocalDate = utc.subtract(tzOffsetMinutes, 'minutes');
    return serverLocalDate.toDate();
  })
}

@Narretz Narretz self-assigned this Nov 15, 2017
Narretz added a commit to Narretz/angular.js that referenced this issue Nov 21, 2017
This commit also fixes a bug where part of the Date object
was re-used even after the input was emptied.

Fixes angular#13382
Closes angular#16181
Narretz added a commit to Narretz/angular.js that referenced this issue Nov 21, 2017
This commit also fixes a bug where part of the Date object
was re-used even after the input was emptied.

Fixes angular#16181
Closes angular#13382
Narretz added a commit to Narretz/angular.js that referenced this issue Dec 13, 2017
This commit also fixes a bug where part of the Date object
was re-used even after the input was emptied.

Fixes angular#16181
Closes angular#13382
Narretz added a commit to Narretz/angular.js that referenced this issue Apr 6, 2018
This commit also fixes a bug where part of the Date object
was re-used even after the input was emptied.

Fixes angular#16181
Closes angular#13382
Narretz added a commit that referenced this issue Apr 6, 2018
This commit also fixes a bug where part of the Date object
was re-used even after the input was emptied.

Fixes #16181
Closes #13382
Closes #16336
Narretz added a commit that referenced this issue Apr 6, 2018
This commit also fixes a bug where part of the Date object
was re-used even after the input was emptied.

Fixes #16181
Closes #13382
Closes #16336
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.