2016-12-11 2 views
1

I18nSelectPipeを使用しようとしています。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> 

、私はパイプが、それはいつも私を返すことを利用し、「キー」を翻訳し、単純にしようとしている私はcode次持っています次のエラー:

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

ありがとうございました。私はちょうど私の頭を叩いた後、私はそれが問題であることを発見したことを思い出した(私は答えを投稿するのを忘れていた):) –

関連する問題