UNPKG

ngx-nullish

Version:

🔨 Angular Structural Directive which replace *ngIf by Nullish Coalescing operator

75 lines (69 loc) • 2.31 kB
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 var NgxNullishDirective = /** @class */ (function () { function NgxNullishDirective(templateRef, viewContainer) { this.templateRef = templateRef; this.viewContainer = viewContainer; this.hasView = false; this.context = { ngxNullish: undefined, $implicit: undefined, }; } NgxNullishDirective.ngTemplateContextGuard = function (dir, ctx) { return true; }; Object.defineProperty(NgxNullishDirective.prototype, "ngxNullish", { set: function (condition) { this.context.$implicit = this.context.ngxNullish = condition; this._updateView(); }, enumerable: true, configurable: true }); NgxNullishDirective.prototype._updateView = function () { 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 = function () { return [ { type: TemplateRef }, { type: ViewContainerRef } ]; }; __decorate([ Input() ], NgxNullishDirective.prototype, "ngxNullish", null); NgxNullishDirective = __decorate([ Directive({ selector: '[ngxNullish]', }) ], NgxNullishDirective); return NgxNullishDirective; }()); var NgxNullishModule = /** @class */ (function () { function NgxNullishModule() { } NgxNullishModule = __decorate([ NgModule({ declarations: [NgxNullishDirective], imports: [], exports: [NgxNullishDirective], }) ], NgxNullishModule); return NgxNullishModule; }()); /* * Public API Surface of ngx-nullish */ /** * Generated bundle index. Do not edit. */ export { NgxNullishDirective, NgxNullishModule }; //# sourceMappingURL=ngx-nullish.js.map