New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ng-let

Package Overview
Dependencies
Maintainers
0
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ng-let

Angular structural directive for sharing data as local variable into html component template.

  • 19.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9.6K
decreased by-15.31%
Maintainers
0
Weekly downloads
 
Created
Source

NgLet Build Status Coverage Status NPM version Maintainability

Angular structural directive for sharing data as local variable into html component template.

Description

Sometime there is a need to share data into component template as local variable. This structural directive create local context of variable that can be used into html template.

See the stackblitz demo.

Features

✅ Observable support
✅ Async pipe support
✅ NgModel support
✅ Type casting

Get Started

Install ng-let

npm i ng-let

Usage, eg.:

import { Component } from '@angular/core';
import { NgLetDirective } from 'ng-let';

@Component({
  selector: 'app-root',
  template: `
  <ng-container *ngLet="(num1 + num2) as total"> <!-- single computation -->
    <div>
      1: {{ total }} <!-- 3 -->
    </div>
    <div>
      2: {{ total }} <!-- 3 -->
    </div>
  </ng-container> 
  `,
  imports: [NgLetDirective]
})
export class AppComponent {
  num1: number = 1;
  num2: number = 2;
}

or with the implicit syntax:

import { Component } from '@angular/core';
import { NgLetDirective } from 'ng-let';

@Component({
  selector: 'app-root',
  template: `
  <ng-container *ngLet="(num1 + num2); let total"> <!-- single computation -->
    <div>
      1: {{ total }} <!-- 3 -->
    </div>
    <div>
      2: {{ total }} <!-- 3 -->
    </div>
  </ng-container> 
  `,
  imports: [NgLetDirective]
})
export class AppComponent {
  num1: number = 1;
  num2: number = 2;
}

Examples

Below there are some examples of use case.

Example: observable

Example of use with observable, eg.:

import { Component } from '@angular/core';
import { defer, Observable, timer } from 'rxjs';
import { NgLetDirective } from 'ng-let';

@Component({
  selector: 'app-root',
  template: `
  <ng-container *ngLet="timer$ | async as time"> <!-- single subscription -->
    <div>
      1: {{ time }}
    </div>
    <div>
      2: {{ time }}
    </div>
  </ng-container>
  `,
  imports: [NgLetDirective]
})
export class AppComponent {
  timer$: Observable<number> = defer(() => timer(3000, 1000));
}

or with the implicit syntax:

import { Component } from '@angular/core';
import { defer, Observable, timer } from 'rxjs';
import { NgLetDirective } from 'ng-let';

@Component({
  selector: 'app-root',
  template: `
  <ng-container *ngLet="timer$ | async; let time"> <!-- single subscription -->
    <div>
      1: {{ time }}
    </div>
    <div>
      2: {{ time }}
    </div>
  </ng-container>
  `,
  imports: [NgLetDirective]
})
export class AppComponent {
  timer$: Observable<number> = defer(() => timer(3000, 1000));
}

Example: signal

Example of use with signal, eg.:

import { Component, signal } from '@angular/core';
import { NgLetDirective } from 'ng-let';

@Component({
  selector: 'app-root',
  template: `
  <ng-container *ngLet="mySignal() as time"> <!-- single computation -->
    <div>
      1: {{ time }}
    </div>
    <div>
      2: {{ time }}
    </div>
  </ng-container>
  `,
  imports: [NgLetDirective]
})
export class AppComponent {
  mySignal = signal(1);

  constructor() {
    setInterval(() => this.mySignal.update(value => value + 1), 1000)
  }
}

or with the implicit syntax:

import { Component, signal } from '@angular/core';
import { NgLetDirective } from 'ng-let';

@Component({
  selector: 'app-root',
  template: `
  <ng-container *ngLet="mySignal(); let time"> <!-- single computation -->
    <div>
      1: {{ time }}
    </div>
    <div>
      2: {{ time }}
    </div>
  </ng-container>
  `,
  imports: [NgLetDirective]
})
export class AppComponent {
  mySignal = signal(1);

  constructor() {
    setInterval(() => this.mySignal.update(value => value + 1), 1000)
  }
}

Support

This is an open-source project. Star this repository, if you like it, or even donate. Thank you so much!

My other libraries

I have published some other Angular libraries, take a look:

Keywords

FAQs

Package last updated on 01 Dec 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc