【问题标题】:Get custom component reference in angular?以角度获取自定义组件参考?
【发布时间】:2017-12-29 15:59:30
【问题描述】:

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

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

在 html 中我将定义如下

<input #myid myattr />

【问题讨论】:

    标签: angular angular2-custom-component


    【解决方案1】:
    @Directive({
        selector: `[myattr]`
    })
    export class myclass
    {
       constructor(private elRef:ElementRef) {}
    
       ngAfterViewInit() {
         console.log(this.elRef.nativeElement);
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 2021-03-05
      • 1970-01-01
      • 2016-11-17
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 2013-07-31
      相关资源
      最近更新 更多