javascript - 在 AngularJS Controller 中获取带有 moment-timezone 的时区名称列表

标签 javascript angularjs timezone momentjs single-page-application

我有一个基于 AngularJS 的 SPA,我正在尝试在 AngularJS Controller 中获取带有时刻时区的时区名称列表。

  • 这是我导入时遇到的错误:“Uncaught TypeError: Cannot read property 'length' of undefined. at addZone (moment-timezone.js:385)”
  • MomentJS 本身工作得很好,即导入时刻(不是时刻时区)
  • 我更喜欢使用纯 Moment,而不是像 Angular 矩这样的任何环绕方式
  • 在 Chrome 浏览器上运行

为什么会发生这个错误? 您能提供一个有效的小代码示例吗?

更新(我正在添加代码):

'use strict';
import moment from 'moment'; // works
import momentTZ from 'moment-timezone'; // this breaks it

export default class RequestCtrl {
    constructor() { 
        console.log(moment()); //works
        //console.log(momentTZ.tz.names());
        //console.log(moment().tz('America/Los_Angeles').format());}

enter image description here

为了单独测试时刻时区,我使用了 JSFiddle。这是 TZ 使用 angularJS 的屏幕截图。不幸的是,它仍然在我的项目中出现问题 enter image description here

最佳答案

如果您希望获取所有时区的列表,则必须导入 moment-timezone 模块。

您可以像我们处理其他模块一样导入和使用 moment-timezone 模块。

其中我们可以使用 moment.tz.names() 获取所有时区列表

确保您的 webpack 配置中有 json loader。看起来时刻时区模块需要这个。

关于javascript - 在 AngularJS Controller 中获取带有 moment-timezone 的时区名称列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48847179/

相关文章:

java - Android如何获取设备时间而不是时区时间

javascript - 未定义值 : angularjs $scope and web3. eth.getBalance

javascript - ExtJS 中的评级表

css - 我如何使用angularjs中的点击事件进入主页?

javascript - 相对于另一个对象对齐一个对象 Angularjs

java - 将时分字符串时间转换为 utc,反之亦然

java - 如何在没有服务器时间的情况下在java中以时区作为输入获取正确的日期和时间

javascript - JavaScript : Pass HTML entities as function parameter

javascript - 编辑记录时显示所选值

javascript - require.js 使用来自不同模块的 AngularJS 指令