How to get DOM elements by class, id, selectors and properties

后端 未结 1 1571
攒了一身酷
攒了一身酷 2020-12-05 14:02

How can I get DOM elements by class, id, selectors (div > .my-i) and properties like in jQuery?

For example:



        
1条回答
  •  Happy的楠姐
    2020-12-05 14:28

    constructor(private elRef:ElementRef) {}
    
    ngAfterViewInit() {
      this.elRef.nativeElement.querySelector('.myClass');
    }
    

    This will not work well with Web Workers or Universal, but Angular2 itself doesn't provide something platform-agnostic to query for elements.

    See also angular 2 / typescript : get hold of an element in the template

    0 讨论(0)
提交回复
热议问题