Structural directive - finding the element it is placed on

前端 未结 4 1820
闹比i
闹比i 2021-02-07 05:25

With a structural directive, how would I get a hold of the (native) element the directive is on? With a normal directive, the ElementRef has it\'s nativeElement pointing to it -

4条回答
  •  渐次进展
    2021-02-07 05:55

    When the structural directive is used on HTMLElement, you will not get the reference to that element, as it is not rendered yet. The input text box is not rendered yet when you are trying to access it in your code console.log(this.el.nativeElement); // template bindings comment

    Please refer the link below to know how structural directives in Angular work. https://angular.io/guide/structural-directives#the-asterisk--prefix

    So to get the reference of the element, you can use attribute directive on the same element and do the things needed.

提交回复
热议问题