Structural directive - finding the element it is placed on

前端 未结 4 1817
闹比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:39

    Based on the Structural Directives Documentation (not sure if it works, but maybe helps you):

    
    
    
    @Directive({
      selector: '[example]'
    })
    export class ExampleDirective {
    
      constructor(private viewContainer: ViewContainerRef) { }
    
      @Input() set example() {
        console.log(this.viewContainer.element.nativeElement.value);
      }
    }
    

提交回复
热议问题