Angularjs 在Angular中动态设置ngModelOptions
我得到了以下片段:Angularjs 在Angular中动态设置ngModelOptions,angularjs,Angularjs,我得到了以下片段: <input type="date" ng-model="arrival" ng-model-options="{timezone: 'PST'}" /> <input type="time" ng-model="arrival" ng-model-options="{timezone: 'PST'}" /> {{arrival}} {{arrival}} 正常工作(日期以从PST转换的UTC时间显示)。我现在正在尝试使“PST”选项动态化: &
<input type="date" ng-model="arrival" ng-model-options="{timezone: 'PST'}" />
<input type="time" ng-model="arrival" ng-model-options="{timezone: 'PST'}" />
{{arrival}}
{{arrival}}
正常工作(日期以从PST转换的UTC时间显示)。我现在正在尝试使“PST”选项动态化:
<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}}
创建另一个具有高优先级(高于ng模型/ng模型选项)的指令(属性类型),该指令监视options对象的更改并触发重新编译。我为缺乏细节而道歉,我正在打电话:)
编辑:
看起来有一个指令名为,它和我描述的完全一样。这里是一个工作,与一些额外的好处,在美国时区的DST因素
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>
和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'
}
];
}]
);
Date.prototype.stdTimezoneOffset=function(){
var jan=新日期(this.getFullYear(),0,1);
var jul=新日期(this.getFullYear(),6,1);
返回Math.max(jan.getTimezoneOffset(),jul.getTimezoneOffset());
}
Date.prototype.dst=函数(){
返回此.getTimezoneOffset()
尝试在ng模型选项中包含一个对象,类似于:ng model options=“options”
和$scope.options={timezone:$scope.timezone}
@NMittal没有任何区别。