# ngx-structurals **Repository Path**: mirrors_TNG/ngx-structurals ## Basic Information - **Project Name**: ngx-structurals - **Description**: Structural utility directives for Angular - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-18 - **Last Updated**: 2026-07-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README [![Build Status](https://travis-ci.com/TNG/ngx-structurals.svg?branch=master)](https://travis-ci.com/TNG/ngx-structurals) [![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-green.svg)](https://conventionalcommits.org) # ngx-structurals Structural utility directives for Angular. The most commonly used [structural directives](https://angular.io/guide/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](#ngxSubscribe) * [*ngxRepeat](#ngxRepeat) * [*ngxAlias](#ngxAlias) * [ngxTemplateContext](#ngxTemplateContext) ### *ngxSubscribe *TL;DR* ``` Emitted: {{ value }} ``` 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: ```

Number of emitted values: {{ count }}

Last emitted value: {{ value }}

Error: {{ error }}

Completed

``` By default, the template on which the directive is applied is used. However, you can also specify different templates for different scenarios: ``` Value: {{ value }} Waiting for first emission… Error: {{ error }} Completed ``` This can be particularly useful for showing loading and error state. ### *ngxRepeat *TL;DR* ``` ``` 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`: ```

Start

Item {{ index }} of {{ count }} is even={{ even }}, odd={{ odd }}

End

``` ### *ngxAlias *TL;DR* ``` {{ data }} ``` 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* ``` {{ data.prop }} ``` Defines the type of the context based on some object. This utility is a workaround for [#28731](https://github.com/angular/angular/issues/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](https://allcontributors.org/docs/en/emoji-key)):

Ingo Bürk

💻

kentkwee

💻

snebjorn

🐛 💻
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! --- [MIT License][license] [license]: https://www.github.com/TNG/ngx-structurals/blob/master/LICENSE