src/lib/directives/form-loading-error.directive.ts
AfterViewInit
OnDestroy
| Selector | [rxapFormLoadingError] |
| Standalone | true |
constructor(formDirective: FormDirective, template: TemplateRef
|
||||||||||||
|
Parameters :
|
import {
AfterViewInit,
Directive,
Inject,
OnDestroy,
TemplateRef,
ViewContainerRef,
} from '@angular/core';
import { Subscription } from 'rxjs';
import { tap } from 'rxjs/operators';
import { FormDirective } from './form.directive';
@Directive({
selector: '[rxapFormLoadingError]',
standalone: true,
})
export class FormLoadingErrorDirective implements AfterViewInit, OnDestroy {
private subscription?: Subscription;
constructor(
@Inject(FormDirective) private readonly formDirective: FormDirective,
@Inject(TemplateRef) private readonly template: TemplateRef<{ $implicit: Error }>,
@Inject(ViewContainerRef) private readonly viewContainerRef: ViewContainerRef,
) {
}
public ngAfterViewInit() {
this.subscription = this.formDirective.loadingError$.pipe(
tap(error => {
this.viewContainerRef.clear();
if (error !== null) {
this.viewContainerRef.createEmbeddedView(this.template, { $implicit: error });
}
}),
).subscribe();
}
public ngOnDestroy() {
this.subscription?.unsubscribe();
}
}