Dynamically Setting ngModelOptions in Angular

Kevin Sylvestre

I've got the following snippet:

<input type="date" ng-model="arrival" ng-model-options="{timezone: 'PST'}" />
<input type="time" ng-model="arrival" ng-model-options="{timezone: 'PST'}" />
{{arrival}}

That works properly (the date is displayed in UTC time converted from PST). I'm now trying to make the 'PST' option dynamic:

<select ng-model="timezone>
  <option value="PST">PST</option>
  <option value="EST">EST</option>
</select>
<input type="date" ng-model="arrival" ng-model-options="{timezone: timezone}" />
<input type="time" ng-model="arrival" ng-model-options="{timezone: timezone}" />
{{arrival}}

However, changing the timezone never update the arrival (it appears that binding doesn't work with nd-model-options). Any way I can force the fields to refresh when the timezone is changed?

Edit

Fiddle: https://jsfiddle.net/10nfqow9/

Marcel Chastain

Create another directive (attribute type) with a high priority (higher than ng-model /ng-model-option's) that watches the options object for changes and triggers a recompile. My apologies for lack of specifics, I'm on a phone :)

EDIT: Looks like there's a directive called kcd-recompile that does exactly what I described. Here's a working plnkr, with some additional goodies for factoring in DST for american timezones.

HTML:

<div kcd-recompile="data.timezone">
  <div>
    <select ng-model="data.timezone" ng-options="x.offset as x.name for x in timezones">
    </select>
  </div>
  <div>
    <input type="date" ng-model="data.arrival" ng-model-options="{timezone: data.timezone}" />
  </div>
  <div>
    <input type="time" ng-model="data.arrival" ng-model-options="{timezone: data.timezone}" />  
  </div>
</div>

And JS:

Date.prototype.stdTimezoneOffset = function() {
    var jan = new Date(this.getFullYear(), 0, 1);
    var jul = new Date(this.getFullYear(), 6, 1);
    return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}

Date.prototype.dst = function() {
    return this.getTimezoneOffset() < this.stdTimezoneOffset();
}

angular.module('DemoApp', ['kcd.directives']);
angular.module('DemoApp')
.controller('DemoCtrl', ['$scope', function($scope) {
    var now = new Date(),
        isDst = now.dst();

    $scope.data ={
      arrival: now,
      timezone: null
    };
    $scope.timezones = [
      {
        name: 'PST', 
        offset: isDst ? '-0700' : '-0800'
      },
      {
        name: 'EST', 
        offset: isDst ? '-0400' : '-0500'
      }
    ];
  }]
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Setting controller dynamically Angular

From Dev

Angular ngModelOptions allowInvalid not working

From Dev

Dynamically setting the a css property of a child in Angular 2

From Dev

Angular2.0.0 - Dynamically setting template url

From Dev

Setting content dynamically inside expansion panel in Angular

From Dev

Angular unit test failed for [ngModelOptions]="{standalone: true}"

From Dev

Setting active tab on dynamically created tabs with Angular UI Bootstrap

From Dev

Adding templateUrl dynamically while setting $routeProvider $ angular.config

From Dev

Dynamically setting CSS property left using width calculation Angular 6

From Dev

What is the complete list of events supported by angular's updateOn property of ngModelOptions?

From Dev

Angular ngModelOptions - bind value to model only when button is clicked

From Dev

Angular ngModelOptions - bind value to model only when button is clicked

From Dev

Dynamically setting Yii layout

From Dev

Dynamically setting passwordMask in Titanium

From Dev

Setting function parameter dynamically

From Dev

Dynamically setting the table layout

From Dev

Setting tableHeaderView height dynamically

From Dev

Setting a variable dynamically in rails

From Dev

Dynamically setting XPath in submission

From Dev

Dynamically setting the table layout

From Dev

Dynamically setting an Image on a ImageButton

From Dev

Setting dates to tabs dynamically

From Dev

Orchard Setting theme dynamically

From Dev

Angular2 use [(ngModel)] with [ngModelOptions]="{standalone: true}" to link to a reference to model's property

From Dev

Setting Control Template Dynamically in WPF

From Dev

Dynamically setting sourceSets in a gradle plugin

From Dev

Setting the Hour and Minute of Timepicker dynamically

From Dev

setting the background color of a view dynamically

From Dev

Dynamically Setting Type of New Instance

Related Related

  1. 1

    Setting controller dynamically Angular

  2. 2

    Angular ngModelOptions allowInvalid not working

  3. 3

    Dynamically setting the a css property of a child in Angular 2

  4. 4

    Angular2.0.0 - Dynamically setting template url

  5. 5

    Setting content dynamically inside expansion panel in Angular

  6. 6

    Angular unit test failed for [ngModelOptions]="{standalone: true}"

  7. 7

    Setting active tab on dynamically created tabs with Angular UI Bootstrap

  8. 8

    Adding templateUrl dynamically while setting $routeProvider $ angular.config

  9. 9

    Dynamically setting CSS property left using width calculation Angular 6

  10. 10

    What is the complete list of events supported by angular's updateOn property of ngModelOptions?

  11. 11

    Angular ngModelOptions - bind value to model only when button is clicked

  12. 12

    Angular ngModelOptions - bind value to model only when button is clicked

  13. 13

    Dynamically setting Yii layout

  14. 14

    Dynamically setting passwordMask in Titanium

  15. 15

    Setting function parameter dynamically

  16. 16

    Dynamically setting the table layout

  17. 17

    Setting tableHeaderView height dynamically

  18. 18

    Setting a variable dynamically in rails

  19. 19

    Dynamically setting XPath in submission

  20. 20

    Dynamically setting the table layout

  21. 21

    Dynamically setting an Image on a ImageButton

  22. 22

    Setting dates to tabs dynamically

  23. 23

    Orchard Setting theme dynamically

  24. 24

    Angular2 use [(ngModel)] with [ngModelOptions]="{standalone: true}" to link to a reference to model's property

  25. 25

    Setting Control Template Dynamically in WPF

  26. 26

    Dynamically setting sourceSets in a gradle plugin

  27. 27

    Setting the Hour and Minute of Timepicker dynamically

  28. 28

    setting the background color of a view dynamically

  29. 29

    Dynamically Setting Type of New Instance

HotTag

Archive