npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tngtech/ngx-structurals

v1.2.2

Published

Structural utility directives for Angular

Downloads

11

Readme

Build Status All Contributors Conventional Commits

ngx-structurals

Structural utility directives for Angular.

The most commonly used structural directives are *ngIf and *ngFor since they are shipped as built-ins with Angular. However, we are not limited to these and can implement our own. This is an often under-appreciate, powerful concept baked into Angular. ngx-structural aims to provide structural directives which may be useful in any Angular project.

How to get it?

Simply install ngx-structurals with the package manager of your choice:

npm install --save @tngtech/ngx-structurals
yarn add @tngtech/ngx-structurals

You can now import NgxStructuralsModule into your application to get access to the directives.

How to use it?

*ngxSubscribe

TL;DR

<ng-container *ngxSubscribe="let data of data$">
    <!-- Note that this prints "null" until data$ emitted a value. -->
    Emitted: {{ value }}
</ng-container>

You can subscribe to an observable directly from the template using *ngxSubscribe. While you can achieve the same thing using *ngIf="data$ | async as data", the latter has a couple of disadvantages:

  1. It fails if data$ emits falsy values such as 0 or null.
  2. There is no way to access error or completion information of the observable.
  3. Rendering is deferred until the observable actually emits.

With *ngxSubscribe all of these points are addressed. Through the template context you have access to all relevant information:

<ng-container *ngxSubscribe="let data of data$; error as error; errored as errored; count as count; completed as completed">
    <p>Number of emitted values: {{ count }}</p>
    <p *ngIf="count > 0">Last emitted value: {{ value }}</p>
    <p *ngIf="errored">Error: {{ error }}</p>
    <p *ngIf="completed">Completed</p>
</ng-container>

By default, the template on which the directive is applied is used. However, you can also specify different templates for different scenarios:

<ng-container *ngxSubscribe="data$; then thenTemplate beforeAny pendingTemplate onError errorTemplate onCompleted completedTemplate">
</ng-container>

<ng-template #thenTemplate let-value>Value: {{ value }}</ng-template>
<ng-template #pendingTemplate>Waiting for first emission…</ng-template>
<ng-template #errorTemplate let-error="error">Error: {{ error }}</ng-template>
<ng-template #completedTemplate>Completed</ng-template>

This can be particularly useful for showing loading and error state.

*ngxRepeat

TL;DR

<ul>
    <li *ngxRepeat="42; let index">Item {{ index }}</li>
</ul>

Renders the given template as many times as specified. This is equivalent of using *ngFor on an array of that length, but avoids having to initialize such an array if you only know the number of items you want to render.

You can also access similar context information as with *ngFor:

<ng-container *ngxRepeat="3; let index; count as count; first as first; last as last; even as even; odd as odd">
    <p *ngIf="first">Start</p>
    <p>Item {{ index }} of {{ count }} is even={{ even }}, odd={{ odd }}</p>
    <p *ngIf="last">End</p>
</ng-container>

*ngxAlias

TL;DR

<ng-container *ngxAlias="data$ | async as data">{{ data }}</ng-container>

Simply renders the given template, but allows aliasing a complex expression to a local template input variable. This is similar to using *ngIf for the same job, but avoids the issues arising from falsy values which would cause the template not to render.

ngxTemplateContext

TL;DR

<ng-template [ngxTemplateContext]="context" let-data>{{ data.prop }}</ng-template>

Defines the type of the context based on some object. This utility is a workaround for #28731 as the context of a template is untyped. The context passed into it is some (possibly empty) object with the appropriate type, e.g.

context?: MyContextType;

Note that this directive only types the context of the directive, but cannot enforce that the context passed to the template actually matches that type.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!


MIT License