![](/img/trans.png)
[英]How to Apply styled using Renderer2 to a child node of ElementRef -> nativeElement in Angular
[英]difference between using angular 2 renderer and using nativeElement
我想知道這段代碼有什么區別:
export class SomeDirective implements OnInit {
constructor(private _elRef: ElementRef){}
ngOnInit(): any {
this._elRef.nativeElement.style.backgroundColor = 'green';
}
}
還有這個:
export class SomeDirective implements OnInit {
constructor(private _elRef: ElementRef, private _renderer: Renderer){}
ngOnInit(): any {
this._renderer.setElementStyle(this._elRef, 'background-color', 'green');
}
}
我知道第二個比第一個有一些優勢,我只需要知道這些優點具體是什么。
我的理解是Renderer
是Angular2中的抽象。 這意味着根據執行上下文插入專用實現。 例如,在瀏覽器中,使用Web worker,從服務器端...
事實上,當DOM API不可用時,存在上下文。 我認為Web工作者和服務器執行。
看到這個問題:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.