Page 439 - AngularJS权威教程
P. 439
31.13 添加时区支持 419
19
20
21
22
23
图31-12 时区支持
24
至此,我们还需要更新MainController内的时间和日期,把新增的时区考虑进去。
以前,匹配时区名对应的格林尼治时间(GMT)偏移量是一项艰巨的任务。Mozilla和Chrome 25
团队使用新的timeZone参数实现了toLocaleString方法,让我们就能够根据时区信息重新映射
日期。由于这里编写的是一个Chrome应用,因此可以在应用中大胆地使用这个函数。 26
回到MainController中,我们还可以基于之前保存的时区信息创建一个新日期:
27
.controller('MainController', function($scope, $timeout, Weather, UserService) {
$scope.date = {};
var updateTime = function() { 28
$scope.date.tz = new Date(new Date().toLocaleString("en-US", {timeZone: $scope.user.
timezone}));
$timeout(updateTime, 1000);
} 29
// ...
现在,我们不再在视图中使用$scope.date.raw了,而是使用$scope.date.tz。时间会伴随 30
时区的修改而变化,如图31-13和图31-14所示。
31
32
33
34
35
图31-13 芝加哥时区 36