0

我有如下自定义组件。我想获取我们在 html 文件中使用的组件的 ID。

@Directive({
    selector: `[myattr]`
})
export class myclass
{
   // want to get reference where myattr defined 
    //#myid
}

在 html 中,我将定义如下

<input #myid myattr />
4

1 回答 1

0
@Directive({
    selector: `[myattr]`
})
export class myclass
{
   constructor(private elRef:ElementRef) {}

   ngAfterViewInit() {
     console.log(this.elRef.nativeElement);
   }
}
于 2017-07-24T06:52:36.920 回答