-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api): use only Renderer2 instances (#391)
NgClass and NgStyle use Renderer1 instances; which has been deprecated. Instead use Renderer2 and a RendererAdapter. Thx @mhevery for his solution! Refs #386.
- Loading branch information
1 parent
6b457dc
commit 816d85a
Showing
3 changed files
with
82 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import {Renderer2, RendererStyleFlags2} from '@angular/core'; | ||
|
||
/** | ||
* Adapts the 'deprecated' Angular Renderer v1 API to use the new Renderer2 instance | ||
* This is required for older versions of NgStyle and NgClass that require | ||
* the v1 API (but should use the v2 instances) | ||
*/ | ||
export class RendererAdapter { | ||
constructor(private _renderer: Renderer2) { } | ||
|
||
setElementClass(el: any, className: string, isAdd: boolean): void { | ||
if (isAdd) { | ||
this._renderer.addClass(el, className); | ||
} else { | ||
this._renderer.removeClass(el, className); | ||
} | ||
} | ||
|
||
setElementStyle(el: any, styleName: string, styleValue: string): void { | ||
if (styleValue) { | ||
this._renderer.setStyle(el, styleName, styleValue); | ||
} else { | ||
this._renderer.removeStyle(el, styleName); | ||
} | ||
} | ||
|
||
// new API is forwarded | ||
addClass(el: any, name: string): void { | ||
this._renderer.addClass(el, name); | ||
} | ||
|
||
removeClass(el: any, name: string): void { | ||
this._renderer.removeClass(el, name); | ||
} | ||
|
||
setStyle(el: any, style: string, value: any, flags?: RendererStyleFlags2): void { | ||
this._renderer.setStyle(el, style, value, flags); | ||
} | ||
|
||
removeStyle(el: any, style: string, flags?: RendererStyleFlags2): void { | ||
this._renderer.removeStyle(el, style, flags); | ||
} | ||
|
||
// ****************************************************************** | ||
// !! Renderer is an abstract class with abstract methods | ||
// | ||
// These are implementation of those methods... and do NOTHING since | ||
// we only use setElementStyle() and setElementClass() | ||
// ****************************************************************** | ||
|
||
/* tslint:disable */ | ||
animate() : any { throw _notImplemented('animate'); } | ||
attachViewAfter() : void { throw _notImplemented('attachViewAfter'); } | ||
detachView() : void { throw _notImplemented('detachView'); } | ||
destroyView() : void { throw _notImplemented('destroyView'); } | ||
createElement() : any { throw _notImplemented('createElement'); } | ||
createViewRoot() : any { throw _notImplemented('createViewRoot'); } | ||
createTemplateAnchor(): any { throw _notImplemented('createTemplateAnchor'); } | ||
createText() : any { throw _notImplemented('createText'); } | ||
invokeElementMethod() : void { throw _notImplemented('invokeElementMethod'); } | ||
projectNodes() : void { throw _notImplemented('projectNodes'); } | ||
selectRootElement() : any { throw _notImplemented('selectRootElement'); } | ||
setBindingDebugInfo() : void { throw _notImplemented('setBindingDebugInfo'); } | ||
setElementProperty() : void { throw _notImplemented('setElementProperty'); } | ||
setElementAttribute() : void { throw _notImplemented('setElementAttribute'); } | ||
setText() : void { throw _notImplemented('setText'); } | ||
listen() : Function { throw _notImplemented('listen'); } | ||
listenGlobal() : Function { throw _notImplemented('listenGlobal'); } | ||
/* tslint:enable */ | ||
} | ||
|
||
function _notImplemented(methodName: string) { | ||
return new Error(`The method RendererAdapter::${methodName}() has not been implemented`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters