ngx-nullish
Version:
🔨 Angular Structural Directive which replace *ngIf by Nullish Coalescing operator
67 lines (61 loc) • 1.86 kB
JavaScript
import { __decorate } from 'tslib';
import { TemplateRef, ViewContainerRef, Input, Directive, NgModule } from '@angular/core';
// Inspired by *ngIf:
// https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_if.ts
let NgxNullishDirective = class NgxNullishDirective {
constructor(templateRef, viewContainer) {
this.templateRef = templateRef;
this.viewContainer = viewContainer;
this.hasView = false;
this.context = {
ngxNullish: undefined,
$implicit: undefined,
};
}
static ngTemplateContextGuard(dir, ctx) {
return true;
}
set ngxNullish(condition) {
this.context.$implicit = this.context.ngxNullish = condition;
this._updateView();
}
_updateView() {
if (this.context.$implicit != null && this.hasView) {
this.viewContainer.createEmbeddedView(this.templateRef, this.context);
this.hasView = false;
}
else if (this.context.$implicit == null && !this.hasView) {
this.viewContainer.clear();
this.hasView = true;
}
}
};
NgxNullishDirective.ctorParameters = () => [
{ type: TemplateRef },
{ type: ViewContainerRef }
];
__decorate([
Input()
], NgxNullishDirective.prototype, "ngxNullish", null);
NgxNullishDirective = __decorate([
Directive({
selector: '[ngxNullish]',
})
], NgxNullishDirective);
let NgxNullishModule = class NgxNullishModule {
};
NgxNullishModule = __decorate([
NgModule({
declarations: [NgxNullishDirective],
imports: [],
exports: [NgxNullishDirective],
})
], NgxNullishModule);
/*
* Public API Surface of ngx-nullish
*/
/**
* Generated bundle index. Do not edit.
*/
export { NgxNullishDirective, NgxNullishModule };
//# sourceMappingURL=ngx-nullish.js.map