To use Renderer, you need a raw HTML element (aka nativeElement). So basically you have to use your whole library, get the source element and pass it to Renderer.
for example
The warning about ElementRef is its use. This means that it is discouraging.
elementRef.nativeElement.style.backgroundColor = 'blue';
But that's fine
renderer.setElementStyle(elementRef.nativeElement, 'background-color', 'blue');
You can also use it with jQuery to show goals.
My recommendation, however, is to use the fact that angular2 allows you to do this easily, regardless of other libraries.
With pure angular2 you have two easy ways
- Using ViewChild with local variables
@Component({ selector : 'my-comp', template : '<h3 #myH3>some text</h3>', }) class MyComp { @ViewChild('myH3') myH3; ngAfterViewInit() { this.renderer.setElementStyle(this.myH3.nativeElement, 'background-color', 'blue'); } }
Here plnkr with all the cases mentioned in this answer. Of course, your requirements may vary, but try to use angular2 whenever you can.
Eric Martinez Jan 19 '16 at 19:03 2016-01-19 19:03
source share