ProductPromotion
Logo

Angular.JS

made by https://0x3d.site

GitHub - nigrosimone/ng-generic-pipe: Generic pipe for Angular application for use a component method into component template
Generic pipe for Angular application for use a component method into component template - nigrosimone/ng-generic-pipe
Visit Site

GitHub - nigrosimone/ng-generic-pipe: Generic pipe for Angular application for use a component method into component template

GitHub - nigrosimone/ng-generic-pipe: Generic pipe for Angular application for use a component method into component template

NgGenericPipe Build Status Coverage Status NPM version

Generic pipe for Angular application for use a component method into component template.

Description

Sometime there is a need to use a component method into component template. Angular best practice says do not use method into html template, eg. {{ myMethod(2) }}. With NgGenericPipe you can use all your public component methods as pure pipe with the component scope (this), eg: {{ 2 | ngGenericPipe: myMethod }}.

See the stackblitz demo.

Features

✅ More than 90% unit tested ✅ Use all your component methods as pure pipe with component scope ✅ Strong type check ✅ Only 658 byte (with gzip compression)

Get Started

Step 1: install ng-generic-pipe

npm i ng-generic-pipe

Step 2: Use ngGenericPipe into your HTML template, eg.:

import { Component } from '@angular/core';
import { NgGenericPipe } from 'ng-generic-pipe';

@Component({
  selector: 'app-root',
  template: `<div>{{ 'Simone' | ngGenericPipe: sayHello }}</div>`,
  imports: [NgGenericPipe]
})
export class AppComponent {
    sayHello(name: string): string {
      return `Hello! I'm ${name}.`; 
    }
}

API

ngGenericPipe need to pipe on a value. The value become the first argument of the function called by ngGenericPipe, eg.:

'Hello world!' | ngGenericPipe: writeMessage

is translated into:

writeMessage('Hello world!')

You can pass, multiple parameter in this way, eg.:

'Hello world!' | ngGenericPipe: writeMessage:'Simone'

is translated into:

writeMessage('Hello world!', 'Simone')

and with more parameters, eg.:

'Hello world!' | ngGenericPipe: writeMessage:'Simone':'Foo':'Bar':'Baz'

is translated into:

writeMessage('Hello world!', 'Simone', 'Foo', 'Bar', 'Baz')

Because ngGenericPipe is a pure pipe, the method is memoized. This means that the pipe transform the html only if an argument change. You can force the change by passing and additional parameter that change when you need a repaint (see the example below "Call component method with component scope and force change detection ").

Strong type check

ngGenericPipe has strong type checking

alt text

Examples

Below there are some examples of use case.

Example: Call component method with component scope

You can call from template a component method test(x: number) and access to the component scope (this), eg.:

import { Component } from '@angular/core';
import { NgGenericPipe } from 'ng-generic-pipe';

@Component({
  selector: 'app-root',
  template: '<div>{{ 3 | ngGenericPipe: test }}</div>',
  imports: [NgGenericPipe]
})
export class AppComponent {

    public y: number = 2;

    test(x: number): number {
      return x * this.y; 
    }
}

Example: Call component method with component scope and multiple parameters

You can call from template a component method test(x: number, z: number) and access to the component scope (this), eg.:

import { Component } from '@angular/core';
import { NgGenericPipe } from 'ng-generic-pipe';

@Component({
  selector: 'app-root',
  template: '<div>{{ 3 | ngGenericPipe: test:3 }}</div>',
  imports: [NgGenericPipe]
})
export class AppComponent {

    public y: number = 2;

    test(x: number, z: number): number {
      return x * this.y * z; 
    }
}

Example: Call component method with component scope and no parameters

You can call from template a component method test() and access to the component scope (this), eg.:

import { Component } from '@angular/core';
import { NgGenericPipe } from 'ng-generic-pipe';

@Component({
  selector: 'app-root',
  template: '<div>{{ undefined | ngGenericPipe: test }}</div>',
  imports: [NgGenericPipe]
})
export class AppComponent {

    public y: number = 2;

    test(): number {
      return this.y; 
    }
}

Example: Call component method with component scope and force change detection

You can call from template a component method test() and access to the component scope (this), eg.:

import { Component } from '@angular/core';
import { NgGenericPipe } from 'ng-generic-pipe';

@Component({
  selector: 'app-root',
  template: `
    <div>{{ undefined | ngGenericPipe: test:i }}</div>
    <button (click)="onUpdate()">Update</button>`,
  imports: [NgGenericPipe]
})
export class AppComponent {

    public y: number = Date.now();
    public i: number = 0;

    test(): number {
      return this.y; 
    }

    onUpdate(){
      this.i++;
    }
}

Example: Call observable component's method

You can call from template a component's method testAsync() that return observable, eg.:

import { Component } from '@angular/core';
import { NgGenericPipe } from 'ng-generic-pipe';

@Component({
  selector: 'app-root',
  template: `<div>{{ 'hello!' | ngGenericPipe: testAsync | async }}</div>`,
  imports: [NgGenericPipe]
})
export class AppComponent {

    testAsync(value: string): Observable<string> {
      return of(value);
    }

}

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:

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory