• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • Angular

Pipes in Angular Part – 4 : SlicePipe, JSONPipe, AsyncPipe

  • Yatendrasinh Joddha
  • August 19, 2018
  • 2 minute read
Angular
Total
0
Shares
0
0
0

Here is the fourth part of angular pipe series in this part we will discuss about SlicePipe, JSONPipe, and AsyncPipe. If you have not read previous articles here is the link for previous articles.

  • Pipes in Angular Part – 1 : lowercase, uppercase, and titlecase pipes
  • Pipes in Angular Part – 2 : DatePipe
  • Pipes in Angular Part – 3 : CurrencyPipe, PercentPipe

SlicePipe

SlicePipe creates the new Array or String containing the subset of given elements. It works same as JavaScript API Array.prototype.slice() and String.prototype.slice()

The slice() with Array returns the selected elements in an array, as a new array object. It will be the copy of the main object event if it is returning whole array.

The slice() with String selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.

Syntax

{{ value_expression | slice : start [ : end ] }}
Let us consider below example for better understanding of this pipe.
app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  animalArray: string[] = ['Tiger', 'Lion', 'Wolf', 'Elephant', 'Monkey'];
}

app.component.html

<div style="text-align:left">
  <ul>
    <li *ngFor = "let obj of animalArray | slice:2:4">{{obj}}</li>
  </ul>
</div>

Output

  • Wolf
  • Elephant

We can also use this pipe for the string variable. Let us consider very short and quick example

//Component
export class AppComponent {
  str: string = 'abcdefg';
}

//HTML
<div style="text-align:left">
    <p> {{str | slice:2:4}}</p>
</div>

//Output: cd

JSONPipe

JSON Pipe is used to convert a value into its JSON format, which is mostly useful in debugging.

Syntax

{{ value_expression | json }}
Consider the below example for the understanding of the usage of this JSON pipe.
app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  obj: object = {name: 'Yatendrasinh', lastName: 'Joddha', Degrees: {Graduations: 'BCA', Masters: 'MCA'}};
}

app.component.html

<div style="text-align:left">
    <p> {{obj | json}}</p>
</div>

//Output: 

{ "name": "Yatendrasinh", "lastName": "Joddha", "Degrees": { "Graduations": "BCA", "Masters": "MCA" } }

AsyncPipe

It is used to opens a value from an asynchronous primitive. The async pipe subscribes to an Observable or Promise and returns the latest value it has produced. When a new value is produced, the async pipe marks the component to be checked for changes. When the component gets destroyed, the async pipe unsubscribes automatically to avoid possible memory leaks.

Syntax

{{ value_expression | async}}

Example

This example binds a Promise to the view. Clicking the Resolve button resolves the promise.

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  greeting: Promise<string>|null = null;
  arrived: boolean = false;
 
  private resolve: Function|null = null;
 
  constructor() { this.reset(); }
 
  reset() {
    this.arrived = false;
    this.greeting = new Promise<string>((resolve, reject) => { this.resolve = resolve; });
  }
 
  clicked() {
    if (this.arrived) {
      this.reset();
    } else {
      this.resolve !('hi there!');
      this.arrived = true;
    }
  }
}

app.component.html

<div>
    <code>promise|async</code>: 
    <button (click)="clicked()">{{ arrived ? 'Reset' : 'Resolve' }}</button>
    <span>Wait for it... {{ greeting | async }}</span>
</div>

Summary

Hope you are in love with reading the content. Don’t forgot to give your reviews. In next and final article of this series we will have a look on Custom Pipe.

Yatendrasinh Joddha
Yatendrasinh Joddha

I have healthy knowledge and experience in Azure, O365 and ASP.Net with c#, MVC, WebApi along with client-side frameworks and libraries like JavaScript, JQuery, KnockoutJs, AngularJs, Angular, ReactJs, NodeJs

Views: 8,555

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0
2 comments
  1. Anonymous says:
    August 19, 2018 at 8:38 pm

    Good one.. really helpful

    Reply
    1. Yatendrasinh Joddha says:
      August 19, 2018 at 8:43 pm

      Thank you 😊

      Reply

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    1 year ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    2 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
  • New Learning Models for Fall 2020

    New Learning Models for Fall 2020

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

%d