繁体   English   中英

angular 2 get组件调用管道

[英]angular 2 get component invoking a pipe

我需要知道哪个组件调用我的管道。

我不想将组件作为管道的参数,例如this {{ "textArg" | myTranslatorPipe:thisTemplate }} {{ "textArg" | myTranslatorPipe:thisTemplate }}

我想要这个{{ "textArg" | myTranslatorPipe }} {{ "textArg" | myTranslatorPipe }}仍然可以访问该组件。

export class myTranslatorPipe implements PipeTransform {
    transform(value: string): string {
        //get component here ?
    }   
}

我将如何实现这一目标? 提前致谢 !

我发现这样做的唯一方法是直接将组件作为参数传递给管道。

变换方法在args接收无限数量的参数,下面的代码就是诀窍:

export class myTranslatorPipe implements PipeTransform {
    transform(value: string, componentRef: any): string {
        //component is stored inside the 'componentRef' 
        if (typeof componentRef === 'undefined' || componentRef === null) {
            throw Error('Missing "componentRef" parameter, correct implementation is "myTranslatorPipe: this".');
        }
        // here you can use your component reference
    }   
}

唯一的事情是你必须以这种方式实现管道{{ "textArg" | myTranslatorPipe: this}} {{ "textArg" | myTranslatorPipe: this}}而不仅仅是{{ "textArg" | myTranslatorPipe }} {{ "textArg" | myTranslatorPipe }}

验证不是强制性的: typeof componentRef === 'undefined' || componentRef === null typeof componentRef === 'undefined' || componentRef === null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM