2015-01-14 86 views
0

我想知道是否有任何选项为angularjs提供的时区设置全局配置。我google了很多,但我找不到可能。在我的应用程序中,我必须将时间传递给服务器,它将存储在MySQL数据库中。虽然节省时间在服务器存储为不同的时间(+8.30),我使用+530时区。他们怎么处理这个问题?设置时区的angularjs全局配置

我的形式发送的日期时间服务器就像如下

2015-01-13T13:45:00.000Z 

没有任何过滤器如何,因为我用很多时间字段来实现这一目标,将复杂的,如果在每一个地方

回答

1
使用

一般来说,使用全局对象在JavaScript中是不好的做法,但angular有一个很好的名称为constant的特性。每个模块都有一个常量功能,您可以在其中存储常量值,并在需要使用它的任何地方注入它。例如,你可以按照这个方法:

angular.module('myApp.config', []) 
.constant('TIME_ZONE','value'); 

angular.module('myApp.controllers', ['myApp.config']) 
.controller('ListCtrl', ['$scope', 'TIME_ZONE', function($scope, time_zone) { 
$scope.time = time_zone; 
}]); 

你可以从这个链接获取模块功能的详细信息: https://docs.angularjs.org/api/ng/type/angular.Module

0

你可以宣布你的应用程序,然后使用constants,您可以将您的应用程序配置时区设置尽可能多的,你想要的。

var myApp = angular.module('myApp', []); 
myApp.constant("myConfig", { 
    "TIMEZONE": "SomeTimezone" 
}); 

myApp.controller('myController', ['myConfig', function(myConfig) { 
    // Access your global timezone using myConfig.TIMEZONE 
}]);