I would like to share some attempts which did not lead me to the desired solution, but maybe could lead someone else to get it working. Also another idea which was not mentioned, yet.
Hook into the templating pipeline
Another way could be to hook into the template creating pipeline of angular.
Idea: Get the AST and check for the pipe class and inject the reference of the element and put it in as a parameter (like mentioned before from others).
For example like these guys did it: https://github.com/typebytes/ngx-template-streams
Attempts:
With pure: true
there is only one instance pipe class created per component.
With pure: false
there is one instance pipe class created per each use of the pipe in your components HTML.
You can inject elementRef: ElementRef
in the constructor of the pipe, but you will only get the reference to the component in which the pipe is used in (in both cases).
I thought maybe it could work somehow with another directive being injected into the pipe which has the reference to the HTMLElement.
Example:
<my-component>
<div>{{'foo' | mypipe}}</div>
<div>{{'foo' | mypipe}}</div>
<input type="text" [value]="'bar' | mypipe">
<input type="text" [value]="'bar' | mypipe">
</my-component>
I tried to create a directive to get a reference for usual HTMLElements which looked the following:
@Directive({
selector: 'input,div,span'
})
export class ElementRefDirective {
constructor(public readonly elementRef: ElementRef) {}
}
But I could not get it injected into the MyPipe class, nor the other way around (getting the MyPipe injected into the directive).