【问题标题】:set angularjs global configuration for timezone为时区设置 angularjs 全局配置
【发布时间】:2015-01-14 08:17:53
【问题描述】:
我想知道是否有任何选项可以为 angularjs 提供的时区设置全局配置。我用谷歌搜索了很多,但我找不到。在我的应用程序中,我必须将时间传递给服务器,它将存储在 mysql db 中。虽然在服务器中节省时间,但它存储为不同的 time(+8.30) ,我使用的是 +530 time zone 。他们怎么处理这个问题?
我的表单作为日期时间发送到服务器,如下所示
2015-01-13T13:45:00.000Z
没有任何过滤器如何实现这一点,因为我使用了许多日期时间字段,如果在每个地方都使用它会很复杂
【问题讨论】:
标签:
javascript
php
mysql
angularjs
【解决方案1】:
一般来说,在 javascript 中使用全局对象是不好的做法,但是 Angular 有一个很好的特性,叫做常量。每个模块都有一个常量特性,您可以在其中存储 const 值并将其注入您需要使用它的任何地方。例如,您可以采用这种方法:
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
【解决方案2】:
您可以声明您的应用程序,然后使用constants为您的应用程序配置时区,您可以设置任意多个。
var myApp = angular.module('myApp', []);
myApp.constant("myConfig", {
"TIMEZONE": "SomeTimezone"
});
myApp.controller('myController', ['myConfig', function(myConfig) {
// Access your global timezone using myConfig.TIMEZONE
}]);