search
HomeWeb Front-endJS TutorialAn article to talk about pipelines (PIPE) in Angular

What is a pipeline (PIPE)? This article will introduce you to the pipeline (PIPE) in Angular, and talk about the methods of built-in pipelines and custom pipelines. I hope it will be helpful to you!

An article to talk about pipelines (PIPE) in Angular

What is a pipeline (PIPE)


PIPE, translated as pipeline. Angular Pipes are a way of writing display value transformations that can be declared in HTML components. Angular pipelines were previously known as filters in AngularJS and since Angular 2 are known as pipes. Pipes take data as input and transform it into the desired output. [Related tutorial recommendations: angular tutorial, Programming Teaching]

Simply put, Angular Pipes can help us convert our input, including strings , integers, dates, etc., are converted into specific formats according to our needs and displayed in the browser. Through interpolation expressions, we can define a pipeline and use it in specific situations. Angular provides us with many different types of pipelines. Of course, you can even customize pipelines.

To give a relatively simple example, date formats are diverse. You can convert between various formats through pipelines, such as converting seconds into hours, minutes and seconds.

An article to talk about pipelines (PIPE) in Angular

Built-in pipes


As mentioned above, Angular provides us with many different types of pipes. They are all built-in pipes of Angular. As for what the built-in pipes are and how to use them, let’s demonstrate them through code below.

Time Pipelinedate

#We create a new Angular project and add a new Date()# to the page ##:

<div>{{data}}</div>
...
export class AppComponent {
  title = &#39;my-app&#39;;
  data = new Date()
}

The page will display the current time:

An article to talk about pipelines (PIPE) in Angular

Then we can use the built-in pipeline to convert a time format , the syntax of the pipeline needs to add the

| symbol after the variable, and declare the pipeline

<div>{{data | date:&#39;yyyy-MM-dd&#39;}}</div>

Here we use the

date pipe. For details about the specific parameters of the pipe, please see Check out the official documentation, which introduces all the time formats it provides for you to convert.

Angular - DatePipe

https://angular.cn/api/common/DatePipe#description

After we set up the pipeline, browse The time in the processor has changed

An article to talk about pipelines (PIPE) in Angular

Other pipelines

Angular also provides other pipelines, such as changing the name of the currency Format of pipe (currency):

An article to talk about pipelines (PIPE) in Angular

Angular - CurrencyPipe

https://angular.cn/api/common/CurrencyPipe

There is also a pipe that converts strings to uppercase (uppercase):

<div>{{&#39;ASDasd&#39; | uppercase }}</div>

An article to talk about pipelines (PIPE) in Angular

##Angular - UpperCasePipe

https: //angular.cn/api/common/UpperCasePipe

Custom pipe

We introduced several built-in pipes above. First, if the built-in pipelines cannot meet our development needs, then Angular also provides us with custom pipelines. You can define a pipeline yourself and define how to transform the input.


We can quickly generate a pipeline through the command line provided by Angular:

ng g p pipes/pipe-name

Here I created a pipeline named test

An article to talk about pipelines (PIPE) in AngularAfter you run the above command, a pipes folder will be created for you under src, which contains your custom pipes

An article to talk about pipelines (PIPE) in AngularThen Angular will automatically introduce these pipes for you in the app, so that you can use custom pipes everywhere in the world.

An article to talk about pipelines (PIPE) in AngularThen you see the pipe under the pipes folder. The pipe is essentially a class. Here, a decorator is used to give it a name of tests.

在这个类里面我们需要去实现 PipeTransfrom 这个接口,也就是需要有 transform 这个方法,在这个方法中,第一个参数就是要放入管道的输入,第二个数据是我们向管道传递的参数,管道中把它放入了一个数组里面。

这个 transform 方法返回什么,我们的页面拿到的就是什么数据,我们先来做一个测试

  transform(value: unknown, ...args: unknown[]): unknown {
    return &#39;tests&#39;;
  }
  ...
  <div>{{&#39;ASDasd&#39; | tests }}</div>

返回一个固定的字符串,并且在页面中去使用它

An article to talk about pipelines (PIPE) in Angular

可以看到返回的结果变成了 tests 字符串,这样我们就可以确定这个方法的返回值就是最终的输出。

接下来我们实现一个简单的字符串超出截取的管道:

  transform(value: string, ...args: number[]): string {
    let defaultLength = 10;
    if((args[0] || defaultLength )< value.length){
      return value.substr(0,args[0] || defaultLength)+&#39;...&#39;
    }else{
      return value
    }
  }
  ...
  <div>{{&#39;sssssssssssssssssssssssssssssss&#39; | tests: 30 }}</div>

这样就能够对字符串进行一个截取并且在尾部添加 ...

An article to talk about pipelines (PIPE) in Angular

总结


本文我们学习了 Angular 中管道的使用,简单的来说管道就是一个方法,可以将你的输入转化为特定的你需要的输出格式,Angular提供给了我们许多的内置管道,当内置管道不满足你的要求的时候,你还可以通过自定义管道来更加灵活的自定义输出格式

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of An article to talk about pipelines (PIPE) in Angular. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool