Angular5中的转换属性参数

地热

我有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参数?

地热

为了达到我们可以使用的目的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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章