【问题标题】:moment.locale in angular module won't work角度模块中的 moment.locale 不起作用
【发布时间】:2016-02-22 16:38:01
【问题描述】:

我对 Angular 非常陌生,并试图修改一段代码。在一个角度模块中,我有这个:

angular.module('xxx')
.factory('$browse', ['xxx', 
  function(xxx) { 
    browse.formatItem = function(item) {
      if (item.creationDate) {
        item.creationDatePretty = moment(item.creationDate).format('MM/DD/YY');
      }
    }

    //...
  }

  //...
])

我要做的就是使用特定的locale 格式化creationDate,而不是对格式进行硬编码。我试图将第 6 行更改为:

var locale = 'xxx'; //a particular locale
moment.locale(locale);
item.creationDatePretty = moment(item.creationDate);

moment.locale(locale) 似乎需要永远运行(或根本不运行?),我无法记录发生的事情以找出问题。任何想法为什么我不能使用moment.locale()

附:我什至尝试过alert(moment.locale());,但这也不起作用。

编辑:语法

【问题讨论】:

  • 我正在使用 moment,这是我如何在英语和德语之间更改语言环境的示例:moment(Date.now()).locale('en').format("dddd, MMMM做 YYYY, h:mm:ss a") 只需将德语的 'en' 更改为 'de'。

标签: javascript angularjs localization momentjs


【解决方案1】:

您应该使用角矩: https://github.com/urish/angular-moment

【讨论】:

  • 实际上,我刚刚意识到我使用的是旧版本的moment,而locale 并没有在那里介绍。无论如何感谢您的帮助
【解决方案2】:

旧版本的moment 支持lang 而不是locale。例如:

moment.lang('fr')

【讨论】:

    猜你喜欢
    • 2018-09-11
    • 2018-06-07
    • 2015-07-05
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多