Created
August 5, 2021 08:56
-
-
Save anhkind/3a3c4b2833d87468deff829497e271ec to your computer and use it in GitHub Desktop.
Angular directive to re-render a template if it detects any changes of the input
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
/** | |
* Example: | |
* | |
* <ng-container *rerender='changingInput'> | |
* this content will be re-rendered everytime `changingInput` changes | |
* </ng-container> | |
*/ | |
import { Directive, | |
Input, | |
TemplateRef, | |
ViewContainerRef } from '@angular/core'; | |
@Directive({ | |
selector: '[rerender]' | |
}) | |
export class RerenderDirective { | |
constructor( | |
private templateRef: TemplateRef<any>, | |
private viewContainer: ViewContainerRef | |
) {} | |
// if detects changes of the input `val`, clear and rerender the view | |
@Input() set rerender(val) { | |
this.viewContainer.clear(); | |
this.viewContainer.createEmbeddedView(this.templateRef); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment