0

我想知道是否可以将 ngx-translate 与共价动态表单一起使用。我的模板如下图所示:

<td-dynamic-forms [elements]="dataElements">
</td-dynamic-forms>

我的 dataElements 数组:

this.dataElements = [
  {
    'name': 'name',
    'label': 'NAME',
    'type': 'text',
    'disabled': true,
    'required': true,
    'default': this.application ? this.application.name : ''
  },
  {
    'name': 'description',
    'label': 'DESCRIPTION',
    'type': 'textarea',
    'required': false,
    'default': this.application ? this.application.description : ''
  },
];

我想使用 ngx-translate 翻译标签。我在其他常规表单中使用相同,但我想知道是否可以在动态表单中使用翻译。

4

1 回答 1

0

您可以在对象中使用 getter 定义 Label 字段,并在方法中执行您需要的操作。创建动态表单时,它会调用 getter 标签并接收本地化值。

要访问翻译服务,您可以将实例添加为对象中的新字段,并使用this.translateServiceinlabel()方法访问它。

    this.dataElements = [
      {
        'name': 'name',
        'type': 'text',
        'disabled': true,
        'required': true,
        'default': this.application ? this.application.name : '',
        'translateService': this.translateService, //add translation service instance to the object 
         get label() {
              return this.translateService.get("NAME").value;//get traslation by key.
            }
      },
      {
        'name': 'description',
        'type': 'textarea',
        'required': false,
        'default': this.application ? this.application.description : '',
        'translateService': this.translateService,
         get label() {
              return this.translateService.get("DESCRIPTION").value;
            }
      },
    ];
于 2018-05-29T05:21:54.360 回答