benjamincharity
1/3/2018 - 7:47 PM

One-time-binding for Angular.

This directive will detach the ChangeDetectorRef simulating AngularJS one-time-binding.

Hello <ng-template oneTime>{{name}}</ng-template>!
@Directive({
  selector: '[oneTime]',
})
export class OneTimeDirective {
  constructor(template: TemplateRef<any>, container: ViewContainerRef, zone: NgZone) {
    zone.runOutsideAngular(() => {
      const view = container.createEmbeddedView(template);
      setTimeout(() => view.detach());
    })
  }
}