簡體   English   中英

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

[英]Angular 2: How to Find Children Element in @ViewChild by CSS Selector?

我想通過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);
  } 
}

以下代碼將為您提供來自view child的子元素

let children = this.myDiv.nativeElement.getElementsByTagName("p");

請參閱plunker代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM