Introduction and usage of Angular pipeline PIPE

Introduction and usage of Angular pipeline PIPE

Preface

PIPE, translated as pipeline. Angular pipes are a way to write transformations of display values ​​that can be declared in HTML components. Angular pipes were previously called filters in AngularJS and are now called pipes since Angular 2. A pipeline takes data as input and transforms it into the desired output.

Angular Pipes takes integers, strings, arrays, and dates as input separated by | and then converts them into the required format and displays them in the browser. In interpolation expressions, you can define pipes and use them as appropriate. There are many types of pipes that you can use in an Angular application.

Built-in pipeline

  • String -> String
    • UpperCasePipe
    • LowerCasePipe
    • TitleCasePipe
  • Number -> String
    • DecimalPipe
    • PercentPipe
    • CurrencyPipe
  • Object -> String
    • JsonPipe
    • DatePipe
  • Tools
    • SlicePipe
    • AsyncPipe
    • I18nPluralPipe
    • I18nSelectPipe

How to use

Uppercase conversion

<div>
  <p ngNonBindable>{{ 'Angular' | uppercase }}</p>
  <p>{{ 'Angular' | uppercase }}</p> <!-- Output: ANGULAR -->
</div>

Date formatting

<div>
  <p ngNonBindable>{{ today | date: 'shortTime' }}</p>
  <p>{{ today | date: 'shortTime' }}</p> <!-- Output: Based on the current time, output format: 10:40 AM -->
</div>

Numeric formatting

<div>
  <p ngNonBindable>{{ 3.14159265 | number: '1.4-4' }}</p>
  <p>{{ 3.14159265 | number: '1.4-4' }}</p> <!-- Output: 3.1416 -->
</div>

JavaScript Object Serialization

<div>
  <p ngNonBindable>{{ { name: 'semlinker' } | json }}</p>
  <p>{{ { name: 'semlinker' } | json }}</p> <!-- Output: { "name": "semlinker" } -->
</div>

Pipeline parameters

A pipeline can accept any number of parameters by appending : and the parameter value after the pipeline name. For example, number: '1.4-4'. If you need to pass multiple parameters, separate them with colons. The specific examples are as follows:

<div>
  <p ngNonBindable>{{ 'semlinker' | slice:0:3 }}</p>
  <p>{{ 'semlinker' | slice:0:3 }}</p> <!-- Output: sem -->
</div>

Pipeline Chain

<div>
  <p ngNonBindable>{{ 'semlinker' | slice:0:3 | uppercase }}</p>
  <p>{{ 'semlinker' | slice:0:3 | uppercase }}</p>
</div>

Custom pipeline

The following uses the pipeline used in previous projects as an example to explain the steps of customizing the pipeline:

  • Use the @Pipe decorator to define the metadata information of the Pipe, such as the name of the Pipe - that is, the name attribute
  • Implement the transform method defined in the PipeTransform interface

definition

import { Pipe, PipeTransform } from "@angular/core";

@Pipe({ name: "formatError" })
export class FormatErrorPipe implements PipeTransform {
    constructor() {}

    transform(value: any, module: string) {
        if (value.code) {
            return value.desc;
        } else {
            return value.message;
        }
    }
}

use

<div *ngIf="errorMessage">
    <div class="message-box error mb-16" [@animate]="{value:'*',params:{opacity:'0',duration:'200ms'}}">
        {{errorMessage.error | formatError:"auth"}}
    </div>
</div>

Summarize

This is the end of this article about Angular pipe PIPE. For more relevant Angular pipe PIPE content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to use Angular2's pipe
  • Angular2 pipeline Pipe and custom pipeline format data usage example analysis

<<:  When adding a Windows 2008 server subdomain to a parent domain, an error message appears: the domain already exists

>>:  Basic JSON Operation Guide in MySQL 5.7

Recommend

Summary of 11 amazing JavaScript code refactoring best practices

Table of contents 1. Extracting functions 2. Merg...

React implements double slider cross sliding

This article shares the specific code for React t...

Detailed explanation of log processing of Docker containers

Docker has many log plug-ins. The default is to u...

Ubuntu 16.04 kernel upgrade steps

1. Environment Ubuntu 16.04 running on a virtual ...

How to create an index on a join table in MySQL

This article introduces how to create an index on...

MySQL high concurrency method to generate unique order number

Preface After this blog post was published, some ...

Nginx forwarding based on URL parameters

Use scenarios: The jump path needs to be dynamica...

Script example for starting and stopping spring boot projects in Linux

There are three ways to start a springboot projec...

Detailed explanation of CSS pre-compiled languages ​​and their differences

1. What is As a markup language, CSS has a relati...

Detailed explanation of JavaScript array deduplication

Table of contents 1. Array deduplication 2. Dedup...

Nginx routing forwarding and reverse proxy location configuration implementation

Three ways to configure Nginx The first method di...

Summary of some reasons why crontab scheduled tasks are not executed

Preface I recently encountered some problems at w...