Angular5中的转换属性参数

时间:2018-12-05 12:11:10

标签: html angular internationalization primeng

我有primeNg multiselect(这是网站上的纯示例,没有添加任何内容)。

在我导入的模块上:

import {MultiSelectModule} from 'primeng/multiselect';

比起我用* .html创建组件的方式:

<p-multiSelect [options]="cities1"
               [(ngModel)]="selectedCities1">
</p-multiSelect>

和* .ts:

import {SelectItem} from 'primeng/api';

interface City {
    name: string,
    code: string
}

export class MyModel {
    cities1: SelectItem[];
    selectedCities1: City[];

    constructor() {
        this.cities1 = [
            {label:'New York', value:{id:1, name: 'New York', code: 'NY'}},
            {label:'Rome', value:{id:2, name: 'Rome', code: 'RM'}},
            {label:'London', value:{id:3, name: 'London', code: 'LDN'}},
            {label:'Istanbul', value:{id:4, name: 'Istanbul', code: 'IST'}},
            {label:'Paris', value:{id:5, name: 'Paris', code: 'PRS'}}
        ];
    }
}

[defaultLabel]="choose"等内置属性是字符串。

<p-multiSelect [options]="cities1"
               [(ngModel)]="selectedCities1"
               [defaultLabel]="choose"
</p-multiSelect>

现在我想根据i18n更改默认标签:

通常在html中,我将i18n用作:

<p>{{'MULTISELECT.DEFAULT' | translate }}</p>

但是它不能像这样工作:

<p-multiSelect [options]="cities1"
               [(ngModel)]="selectedCities1"
               [defaultLabel]="{{'MULTISELECT.DEFAULT' | translate }}"
</p-multiSelect>

有什么想法如何将转换值传递给property参数?

1 个答案:

答案 0 :(得分:0)

为实现这一点,我们可以使用translateService: TranslateService。由import { TranslateService } from '@ngx-translate/core';

导入

我们可以将其用作:

this.translateService.get("MULTISELECT.DEFAULT").subscribe((translatedLabel: string) => {
      this.translatedMultiSelectDefaultLabel= translatedLabel;
    });

,而不仅仅是分配为:

<p-multiSelect [options]="cities1"
               [(ngModel)]="selectedCities1"
               [defaultLabel]="translatedMultiSelectDefaultLabel"
</p-multiSelect>