Skip to content

Instantly share code, notes, and snippets.

@anhkind
Created August 5, 2021 08:56
Show Gist options
  • Save anhkind/3a3c4b2833d87468deff829497e271ec to your computer and use it in GitHub Desktop.
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
/**
* 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