Angular 2:如何通过CSS Selector在@ViewChild中查找子元素?

Bad*_*bet 4 angular2-template angular

我想通过nth-child()选择器或类改变第二个p的样式:

import { Component, ViewChild } from '@angular/core';
@Component({
  selector: 'my-app',
  template: `
    <div #container>
        <p>para 1</p>
        <p class="my-class">para 2</p>
        <button>button 1</button>
    </div>
  `
})
export class AppComponent {
  @ViewChild('container') myDiv;
  ngAfterViewInit(){
    console.log(this.myDiv.nativeElement.children);
  } 
}
Run Code Online (Sandbox Code Playgroud)

Roh*_*ing 5

以下代码将为您提供来自view child的子元素

let children = this.myDiv.nativeElement.getElementsByTagName("p");
Run Code Online (Sandbox Code Playgroud)

请参阅plunker代码

https://plnkr.co/edit/yX2jIG?p=preview