【问题标题】:nativeElement of ViewChildren's first is undefinedViewChildren's first 的 nativeElement 未定义
【发布时间】:2018-04-19 09:23:35
【问题描述】:

我正在尝试访问 ViewChildren 的本机元素 => HTMLElements,但这样做时我不断收到 undefined 响应。看看:

list.html

..
<app-element #someElement *ngFor="let element of elements"  [someinput]="someinput"></app-element>
..

list.ts

  @ViewChildren("someElement") someElements:QueryList<ElementRef>;

  @HostListener("window:resize", ["$event"]) 
  resize(event){
    console.log(event);
    console.log((this.someElements.first)); //returns ElementComponent as expected
    console.log((this.someElements.first.nativeElement)); // nativeElement appears to be undefined

  }

我已经多次使用ViewChildren。但是,这一次它没有按预期工作。是因为ViewChild 是一个角度分量吗?我错过了什么明显的东西吗?

【问题讨论】:

    标签: angular


    【解决方案1】:

    我会指定我想要得到的东西

    @ViewChildren("someElement", { read: ElementRef }) someElements:QueryList<ElementRef>;
                                 ^^^^^^^^^^^^^^^^^^^^^
                           please give me ElementRef instead of component instance
    

    另见

    【讨论】:

    • 那里你可以找到另一个很好的答案的链接)我建议你在空闲时间阅读它
    猜你喜欢
    • 2018-02-05
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 2018-02-26
    • 2017-12-15
    • 2017-09-28
    • 1970-01-01
    相关资源
    最近更新 更多