Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  171] [ 1]  / answers: 1 / hits: 8112  / 8 Years ago, mon, august 8, 2016, 12:00:00

I want to pass an element reference to a directive. I know that the reference of the element on which the directive has been applied can be obtained by



private _elemRef: ElementRef


but I want to pass the reference to other element to the directive. Any help is appreciated.



Here's the demo code. I m using a ripple directive.



<ul #other>
<li ripple>Hello</li>
</ul>


directive.js



@Directive({
selector: '[ripple]'
})
export class RippleDirective {
constructor(private _elemRef: ElementRef) {
}

@HostListener('click', ['$event'])
public onClick(event: MouseEvent) {
// I wan't to refer the '#other' node here
}
}

More From » angular

 Answers
10

You can pass the template variable #other to an @Input():



@Directive({
selector: '[ripple]'
})
export class RippleDirective {
@Input() ripple;

@HostListener('click', ['$event'])
public onClick(event: MouseEvent) {
this.ripple...
}
}




<ul #other>
<li [ripple]=other>Hello</li>
</ul>

[#26791] Sunday, August 7, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
luizc

Total Points: 623
Total Questions: 87
Total Answers: 103

Location: Zimbabwe
Member since Sat, Feb 27, 2021
3 Years ago
;