Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angular 2获取调用管道的组件_Angular_Typescript - Fatal编程技术网

angular 2获取调用管道的组件

angular 2获取调用管道的组件,angular,typescript,Angular,Typescript,我需要知道哪个组件调用我的管道 我不想将组件作为管道的参数,例如 {{“textArg”| myTranslatorPipe:thisTemplate} 我想要这个{“textArg”| myTranslatorPipe}并且仍然访问组件 export class myTranslatorPipe implements PipeTransform { transform(value: string): string { //get component here ?

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

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

我想要这个
{“textArg”| myTranslatorPipe}
并且仍然访问组件

export class myTranslatorPipe implements PipeTransform {
    transform(value: string): string {
        //get component here ?
    }   
}
我将如何做到这一点?
提前谢谢

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

transform方法在
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}


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

您的目的是什么?您想获取组件名称还是
componentRef
?yurzui:最好是组件参考:)我想
componentRef
可以注入管道中。它可以注入组件,然后应该能够注入管道。它们使用相同的注入器。为什么需要
部件
内管。。?管道只是根据提供给它的集合/值调整值…Pankaj Parkar:因为我想根据调用组件给出不同的结果。我不想将组件指定为参数。