如何在@ViewChild Angular2中查找子元素?

Vic*_*nak 6 angular

import { Component, ViewChild } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
    <div #container>
        <p>para 1</p>
        <p>para 2</p>
        <button>button 1</button>
    </div>
  `
})
export class AppComponent {
  @ViewChild('container') myDiv;

  ngAfterViewInit(){
    //this console.log would print this [p, p, button]
    console.log(this.myDiv.nativeElement.children);
    //how can I access to only p nativeElement only? 
  } 

}
Run Code Online (Sandbox Code Playgroud)

plunker

yur*_*zui 6

您可以尝试以下方法来获取p元素数组:

Array.from(this.myDiv.nativeElement.children).filter(tag => tag.tagName === 'P')
Run Code Online (Sandbox Code Playgroud)