【问题标题】:Angular 2 - Passing an argument to a custom pipeAngular 2 - 将参数传递给自定义管道
【发布时间】:2017-07-30 11:08:26
【问题描述】:

我创建了一个可以接收 2 个参数的管道,但我不确定如何发送它们。

这是我的烟斗:

export class TranslationPipe implements PipeTransform {

    private capitalize: boolean;

    constructor(
        private translationService: TranslationService
    ) {
        this.capitalize = true;
    }

    transform(key: string, capitalize?: boolean): string {
        if (typeof capitalize !== "undefined" || capitalize !== null)
            this.capitalize = capitalize;

        return this.translationService.getTranslation(key, this.capitalize);
    }
}

这是我的 HTML

{{ 'searchquery' | translate }}

这可行,但我怎样才能通过capitlize = false 呢?我尝试了一些谷歌搜索,但我真的找不到任何我想要实现它的示例(也许我做错了?)

感谢您的帮助!

【问题讨论】:

    标签: angular typescript pipe


    【解决方案1】:
    {{ 'searchquery' | translate:false }}
    
    {{ 'searchquery' | translate:'toUsEn' }}
    

    【讨论】:

    • 这行得通,如果我想传递一个字符串,我该怎么做? {{ 'searchquery' | translate:stringvalue }} 似乎不起作用
    • @Nicolas,我知道你可能已经解决了你的问题,但是,由于我有同样的疑问,我想有人可能也会读到这个。所以你只是忘了把你的字符串值放在单引号之间: {{ 'searchquery' |翻译:'stringvalue' }}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2018-06-14
    • 1970-01-01
    • 2018-03-18
    • 2017-05-03
    • 1970-01-01
    相关资源
    最近更新 更多