Socket
Socket
Sign inDemoInstall

@avine/ng-if-non-nullish

Package Overview
Dependencies
5
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @avine/ng-if-non-nullish

Nullish coalescing operator as Angular structural directive and more...


Version published
Maintainers
1
Install size
110 kB
Created

Readme

Source

IfNonNullish

Nullish coalescing operator as Angular structural directive and more...

Demo

Check out demo here.

Installation

Import IfNonNullishDirective (standalone directive) in your app.module.ts:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IfNonNullishDirective } from '@avine/ng-if-non-nullish';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, IfNonNullishDirective],
  bootstrap: [AppComponent],
})
export class AppModule {}

Use ifNonNullish directive in your app.component.ts:

import { Component } from '@angular/core';
import { interval, Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Component({
  selector: 'app-root',
  template: `<div *ifNonNullish="data$ | async as value">{{ value }}</div>`,
})
export class AppComponent {
  data$: Observable<false | null> = interval(1000).pipe(map((i) => (i % 2 ? false : null)));
}

Unlike ngIf directive, falsy values like false, "" or 0 are rendered. Only nullish values like null or undefined are not rendered.

So, in the example above, false will be rendered while null will not.

Usage

Rendering data

Render data using "as" syntax or "implicit" syntax.

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <div *ifNonNullish="data">{{ data }}</div>
    <div *ifNonNullish="data as value">{{ value }}</div>
    <div *ifNonNullish="data; let value">{{ value }}</div>
  `,
})
export class AppComponent {
  data: number | null = 0;
}

Rendering fallback template

Use fallback: input to provide a templateRef when data is nullish.

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <div *ifNonNullish="data as value; fallback: fallbackTemplate">
      {{ value }}
    </div>
    <ng-template #fallbackTemplate>
      <i>Fallback</i>
    </ng-template>
  `,
})
export class AppComponent {
  data = undefined;
}

License

MIT

Keywords

FAQs

Last updated on 19 Sep 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc