Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nigrosimone/ng-condition

An alternative to `*ngIf; else` directive for simplify conditions into HTML template for Angular application.
https://github.com/nigrosimone/ng-condition

angular angular2

Last synced: 3 months ago
JSON representation

An alternative to `*ngIf; else` directive for simplify conditions into HTML template for Angular application.

Awesome Lists containing this project

README

        

# NgCondition [![Build Status](https://travis-ci.com/nigrosimone/ng-condition.svg?branch=master)](https://travis-ci.com/nigrosimone/ng-condition) [![Coverage Status](https://coveralls.io/repos/github/nigrosimone/ng-condition/badge.svg?branch=master)](https://coveralls.io/github/nigrosimone/ng-condition?branch=master) [![NPM version](https://img.shields.io/npm/v/ng-condition.svg)](https://www.npmjs.com/package/ng-condition)

An alternative to `*ngIf; else` directive for simplify conditions into HTML template for Angular application.

## Description

Sometime multiple `*ngIf; else` can make html template ugly and complicated to understand, eg.:

```html


Number is minor or equal 5


Number is minor or equal 10



Number is minor or equal 20


Number is major of 20



```

with `ng-condition` you have a simple if, else if, else block, and the same template become eg.:

```html


Number is minor or equal 5


Number is minor or equal 10


Number is minor or equal 20


Number is major of 20

```

See the [stackblitz demo](https://stackblitz.com/edit/demo-ng-condition?file=src%2Fapp%2Fapp.component.ts).

## Get Started

*Step 1*: install `ng-condition`

```bash
npm i ng-condition
```

*Step 2*: Import `NgConditionModule` into your app module, eg.:

```ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';

import { NgConditionModule } from 'ng-condition';

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

## Examples

Below there are some examples of use case.

### Example: Simple if, else

Example of simple if, else, eg.:

```html


number is even


number is odd

```

### Example: Nested condition

Nested condition, eg.:

```html


number is even


Number is minor or equal 5


Number is major of 5



number is odd


Number is minor or equal 10


Number is major of 10


```

## Support

This is an open-source project. Star this [repository](https://github.com/nigrosimone/ng-condition), if you like it, or even [donate](https://www.paypal.com/paypalme/snwp). Thank you so much!

## My other libraries

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

- [NgSimpleState: Simple state management in Angular with only Services and RxJS](https://www.npmjs.com/package/ng-simple-state)
- [NgHttpCaching: Cache for HTTP requests in Angular application](https://www.npmjs.com/package/ng-http-caching)
- [NgGenericPipe: Generic pipe for Angular application for use a component method into component template.](https://www.npmjs.com/package/ng-generic-pipe)
- [NgLet: Structural directive for sharing data as local variable into html component template](https://www.npmjs.com/package/ng-let)
- [NgForTrackByProperty: Angular global trackBy property directive with strict type checking](https://www.npmjs.com/package/ng-for-track-by-property)