使用 i18n select 管道
Using i18n select pipe
我正在尝试使用 I18nSelectPipe
。我有以下 code
:
this.criteria = [];
this.criteria.push({
id: 1,
name: 'ab'
});
this.criteria.push({
id: 2,
name: 'cd'
});
this.criteria.push({
id: 3,
name: 'ef'
});
this.criteriaChoice = {};
this.criteriaChoice['1'] = 'One';
this.criteriaChoice['2'] = 'Two';
this.criteriaChoice['3'] = 'Three';
在HTML
中:
<div *ngFor="let criterium of criteria">
<strong>{{criterium.id | i18nSelect: criteriaChoice}}</strong>
</div>
您可能已经注意到,我正在尝试使用 管道 简单地翻译 "key",但它总是 returns 我出现以下错误:
ORIGINAL EXCEPTION: Invalid argument '[object Object]' for pipe
'I18nSelectPipe'
如果我尝试如下简单的操作,它会起作用:
<strong>{{'1' | i18nSelect: criteriaChoice}}</strong>
我该如何解决这个问题?
顺便说一下,这是一个示例代码,代码不是这样硬编码的。
尝试:
<strong>{{criterium.id+'' | i18nSelect: criteriaChoice}}</strong>
我在使用 Angular 2.4.10.
的布尔值时遇到了同样的问题
当我查看代码时:
I18nSelectPipe.prototype.transform = function (value, mapping) {
if (value == null)
return '';
if (typeof mapping !== 'object' || typeof value !== 'string') {
throw new InvalidPipeArgumentError(I18nSelectPipe, mapping);
}
if (mapping.hasOwnProperty(value)) {
return mapping[value];
}
if (mapping.hasOwnProperty('other')) {
return mapping['other'];
}
return '';
};
我注意到通过管道输入的 value
必须是一个字符串。当我使用 +''
将 value
转换为字符串时,错误消失了。
我正在尝试使用 I18nSelectPipe
。我有以下 code
:
this.criteria = [];
this.criteria.push({
id: 1,
name: 'ab'
});
this.criteria.push({
id: 2,
name: 'cd'
});
this.criteria.push({
id: 3,
name: 'ef'
});
this.criteriaChoice = {};
this.criteriaChoice['1'] = 'One';
this.criteriaChoice['2'] = 'Two';
this.criteriaChoice['3'] = 'Three';
在HTML
中:
<div *ngFor="let criterium of criteria">
<strong>{{criterium.id | i18nSelect: criteriaChoice}}</strong>
</div>
您可能已经注意到,我正在尝试使用 管道 简单地翻译 "key",但它总是 returns 我出现以下错误:
ORIGINAL EXCEPTION: Invalid argument '[object Object]' for pipe 'I18nSelectPipe'
如果我尝试如下简单的操作,它会起作用:
<strong>{{'1' | i18nSelect: criteriaChoice}}</strong>
我该如何解决这个问题?
顺便说一下,这是一个示例代码,代码不是这样硬编码的。
尝试:
<strong>{{criterium.id+'' | i18nSelect: criteriaChoice}}</strong>
我在使用 Angular 2.4.10.
的布尔值时遇到了同样的问题当我查看代码时:
I18nSelectPipe.prototype.transform = function (value, mapping) {
if (value == null)
return '';
if (typeof mapping !== 'object' || typeof value !== 'string') {
throw new InvalidPipeArgumentError(I18nSelectPipe, mapping);
}
if (mapping.hasOwnProperty(value)) {
return mapping[value];
}
if (mapping.hasOwnProperty('other')) {
return mapping['other'];
}
return '';
};
我注意到通过管道输入的 value
必须是一个字符串。当我使用 +''
将 value
转换为字符串时,错误消失了。