【问题标题】:ng2-translate directive not working while pipe isng2-translate 指令在管道运行时不起作用
【发布时间】:2017-05-08 19:59:09
【问题描述】:

Ng2-translate directive 给我带来了一些问题,但我不知道我做错了什么或者这是一个错误。

在同一组件的 html 模板上,翻译 pipe 正在工作,而翻译 directive 不工作。

特别是:

<span translate>{{ feature.linkTxt }}</span> // works

<span [translate]="feature.linkTxt"></span> // not working

https://github.com/ocombe/ng2-translate#4-use-the-service-the-pipe-or-the-directive

我错过了什么还是应该工作?

编辑

变量feature.linkTxt指向stringLEARNMORE,对应如下json字符串:

"LEARNMORE": "Learn more",

如果我从指令切换到管道,使用相同的变量,将显示翻译后的文本。

feature.linkTxt 变量由以下*ngFor 语句生成:

<li *ngFor="let feature of featureslist"> ...

【问题讨论】:

  • 您在使用时遇到任何错误吗?
  • @ranakrunal9 没有任何错误,文本没有翻译,DOM 元素没有文本
  • 你能分享你的语言json文件对象吗?

标签: angular angular-directive ng2-translate angular-pipe


【解决方案1】:

根据ng2-translate example,如果您的语言文件如下:

{
  HELLO: 'hello {{value}}'
}

那么你应该像这样使用它:

// there is single quote foe HELLO inside [translate]
<div [translate]="'HELLO'" [translateParams]="{value: 'world'}"></div>

所以我认为你的 json 就像:{ feature : { linkTxt : 'Your Text' } } 并且你应该像这样使用它:

<span [translate]="'feature.linkTxt'"></span>

【讨论】:

  • 在我的例子中,feature.linkTxt 是一个在ngFor 循环中生成的变量,这就是为什么我周围没有' '
  • 你试过&lt;span translate&gt;feature.linkTxt&lt;/span&gt;吗?
  • feature.linkTxt 是一个变量,所以我尝试了&lt;span translate&gt;{{ feature.linkTxt }}&lt;/span&gt; 并且有效。显然这是一个错误,我正在为此添加响应。
【解决方案2】:

事实证明这是一个已知的开放错误。

https://github.com/ocombe/ng2-translate/issues/355

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    相关资源
    最近更新 更多