简体   繁体   English

将元素引用传递给指令

[英]pass element reference to a directive

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. 我正在使用ripple指令。

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

directive.js 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
}
} 

You can pass the template variable #other to an @Input() : 您可以将模板变量#other@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>

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

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