【问题标题】:Using i18n select pipe使用 i18n 选择管道
【发布时间】:2017-04-26 05:31:13
【问题描述】:

我正在尝试使用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>

您可能已经注意到,我正在尝试使用该 管道 简单地翻译“密钥”,但它总是返回以下错误:

原始异常:管道的参数“[object Object]”无效 'I18nSelectPipe'

如果我尝试以下简单的方法,它会起作用:

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

我该如何解决这个问题?

顺便说一下,这是一个示例代码,代码不是这样硬编码的。

【问题讨论】:

    标签: angular angular2-pipe


    【解决方案1】:

    试试:

    &lt;strong&gt;{{criterium.id+'' | i18nSelect: criteriaChoice}}&lt;/strong&gt;

    我在使用 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 转换为字符串时,错误消失了。

    【讨论】:

    • 谢谢。我只记得在我撞了几天头之后,我才发现这是问题所在(我忘了发布答案):)
    猜你喜欢
    • 2019-09-28
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    相关资源
    最近更新 更多