2016-10-21 37 views
-1

我使用ElementRef获取组件的DOM对象。使用elementRef [“nativeElement”]我像这个 -在Angular2中迭代ElementRef

<selectorName> 
    <input type="text" /> 
    ...... 
</selectorName> 

我想要遍历所有的HTML对象来获取元素类型和ID。任何人都可以告诉我如何做到这一点。

+0

请提供更多的上下文。你为什么想这样做。 HTML从哪里来?你有哪些代码使用'ElementRef'。 –

+0

我已经在构造函数中定义了ElementRef。我正在使用API​​来生成需要Html对象的UI表单,所以我想迭代html对象并将其传递给API。 – deen

+0

请添加更多代码来演示所有这些。 –

回答

1
constructor(private elRef:ElementRef) {} 

ngAfterViewInit() { 
    let elements = this.elRef.nativeElement.querySelectorAll('someSelector'); 
}