Use ElementRef
ElementRef will insert the current DOM node into your component. The ElementRef service will always be local to your current DOM node.
By entering it, you can get the actual DOM node using nativeElement:
var el = elementRef.nativeElement
Once you do this, you can manipulate it the way you like, using DOM scripting or using a DOM wrapper such as jQuery:
$(el) .append('<p>Some interesting Stuff') .addClass('my_class');
But don't do it if you can help him
Remember that, as with Angular 1, using direct DOM manipulation is not recommended. You can get almost all of your work using templates, and you should support this way of working most of the time.
Direct manipulation of the DOM leads to complex, difficult to understand, difficult testing.
There are times when this may seem like the best solution. For example, if you need to integrate with a third-party structure with a high degree of complexity, for example, with a graphics library.
Here is an example (in ES6)
var AppComponent = ng.core .Component({ selector: "app", template: ` <div>Captured element</div> ` }) .Class({ constructor: [ng.core.ElementRef, function(elementRef) { var el = elementRef.nativeElement el; }] })
superluminary Feb 27 '16 at 18:15 2016-02-27 18:15
source share