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?
}
}
参见 plunker
最佳答案
您可以尝试以下方法来获取 p
元素的数组:
Array.from(this.myDiv.nativeElement.children).filter(tag => tag.tagName === 'P')
关于angular - 如何在@ViewChild Angular2 中查找子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38149322/