【问题标题】:angular $translate.instant returns object instead of string角度 $translate.instant 返回对象而不是字符串
【发布时间】:2017-04-19 08:06:40
【问题描述】:

我正在使用 "angular-translate": "^2.15.1"$translateProvider.useSanitizeValueStrategy('sce')

在我的控制器中,我需要将翻译后的字符串值传递给 $http 服务。

但下面的代码返回给我TrustedValueHolderType 对象而不是字符串。

defaultMessage = translate.instant('welcome-message.default.message');

最低限度的 plunkr https://plnkr.co/edit/ZPtixjNub9kIExiuGIkp?p=preview 打开控制台(清除所有)。然后点击 DE 或 EN 按钮并查看日志

【问题讨论】:

  • 你能创建一个 plunker 复制这个吗?
  • @tanmay plunker,请点击DE或EN打开控制台查看日志:plnkr.co/edit/ZPtixjNub9kIExiuGIkp?p=preview
  • 不知道如何但是,decodeURIComponent() 解决了这个问题

标签: angularjs internationalization angular-translate


【解决方案1】:

是的,正如评论中提到的,decodeURIComponent 可能会解决它。但首先造成这种情况的原因如下:

$translateProvider.useSanitizeValueStrategy('sce')

这会将您的输出编码为全局$translate。这就是为什么您可以使用decodeURIComponent 来解决它。

也许您可以使用不同的策略。 angular-translate guide 似乎为此建议了 escape 策略。

这是一个forked plunker,可以与escape 策略一起使用,而无需使用decodeURIComponent

【讨论】:

    猜你喜欢
    • 2018-08-22
    • 2017-07-18
    • 2011-07-29
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多