【问题标题】:How to pass values for translation file to ngfor in angular8如何在角度 8 中将翻译文件的值传递给 ngfor
【发布时间】:2019-09-12 13:44:42
【问题描述】:

我的翻译 (json) 文件中有一些文本,我想通过组件中的 *ngFor 传递这些文本。

我喜欢用*ngFor* 替换该代码:

  <div class="container padding-top-3">
      <div class="segmented-controls-group">
        <span class="filter__intro">
          <i class="icon icon-253-filter icon--s2" aria-hidden="true"></i>{{'COMMONS.TAG.FILTER'|translate}}
        </span>
        <input type="radio" id="segmented-controls1-1" class="tag">
        <label for="segmented-controls1-1" tabindex="0">Marketing</label>
        <input type="radio" id="segmented-controls1-2" class="tag">
        <label for="segmented-controls1-2" tabindex="0">TV</label>
        <input type="radio" id="segmented-controls1-3" class="tag">
        <label for="segmented-controls1-3" tabindex="0">Sponsoring</label>
      </div>
  </div>

有人可以支持我吗?

【问题讨论】:

  • 你的 json 是什么样的?你有没有尝试过什么?为什么它不起作用?

标签: json angular ngfor


【解决方案1】:

您可以使用 JSON.Parse(json) 将您的 json 转换为 object

然后像任何数组一样迭代它

public jsonToObject(string json): any{
    return JSON.Parse(json);
}

我认为这会起作用。

【讨论】:

    【解决方案2】:

    你的意思是这样吗?

    <ng-container *ngFor="let key of ['MARKETING', 'TV', 'SPONSORING']; let i = index">
      <input  type="radio" [attr.id]="'segmented-controls1-' + i" class="tag" name="tag">
      <label [attr.for]="'segmented-controls1-' + i" tabindex="0">
       {{('COMMONS.TAG.' + key) | translate}}
      </label>
    </ng-container>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 2020-07-30
      • 2018-09-03
      • 1970-01-01
      • 2020-08-21
      相关资源
      最近更新 更多