2016-12-11 30 views
1

我正在尝试使用I18nSelectPipe。我有以下code使用i18n选择管道

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> 

正如你可能已经注意到,我想简单地翻译了“钥匙”使用,但它总是返回我以下错误:

ORIGINAL EXCEPTION: Invalid argument '[object Object]' for pipe 'I18nSelectPipe'

如果我尝试一些如下的简单,它的工作原理:

<strong>{{'1' | i18nSelect: criteriaChoice}}</strong> 

我该如何解决这个问题?

顺便说一下,这是一个示例代码,代码没有硬编码这种方式。

回答

2

尝试:

<strong>{{criterium.id+'' | i18nSelect: criteriaChoice}}</strong>

我有同样的问题,采用了棱角分明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铸造成字符串时,错误消失了。

+0

谢谢。我只记得有一段时间我敲着脑袋后,我才发现这是问题(我忘了发表回答):) –