How to get DOM elements by classes, identifiers, selectors and properties

How can I get DOM elements by classes, identifiers, selectors ( div > .my-i ) and properties, for example, in jQuery?

For example:

 <div class="myClass" style="width:200px"> <span id="pop">test</span> <div> <i>aaa</i> <i class="my-i">bbb</i> <i class="my-i">ccc</i> </div> 

I need to get:

  • 200px class value
  • text 'test' by id
  • all class = "my-i"

What is the best way?

+8
angular
Nov 23 '16 at 8:28
source share
1 answer
 constructor(private elRef:ElementRef) {} ngAfterViewInit() { this.elRef.nativeElement.querySelector('.myClass'); } 

This will not work with web workers or Universal, but Angular2 itself does not provide something agnostic for requesting elements.

See also angular 2 / typescript: get element in template

+20
Nov 23 '16 at 8:44
source share



All Articles