Angular Slice Pipe Example

Angular Slice Pipe Example

In Angular, We have a few built-in pipes. In this blog post, we’ll learn about slice pipe in Angular.

We’ll see an example of using slice pipe with Array and String Expression.

In Angular, Pipe is something that takes in data as input and transforms it to the desired output.

SlicePipe

SlicePipe is an API provided by angular. It is part of angular CommonModule.
It creates a new Array or String containing a subset(slice) of the elements.
SlicePipe is using Javascript API such as Array.prototype.slice() and String.prototype.slice() under the hood.
SlicePipe is Parameterized Pipe. Parameterized Pipe accepts any number of optional parameter to get the desired output from the given input.
To add parameters to a pipe, follow the pipe name with a colon ( : ) and then the parameter value (such as slice:1). 
If the pipe accepts multiple parameters, separate the values with colons (such as slice:1:5).


{{ value_expression | slice : start [ : end ] }}

The basic format of SlicePipe is as above.

Now we’ll see each fragment one by one:

value_expression: It’s the input list or string to be sliced.
pipe operator( | ) : In angular, this is known as pipe operator, which tells angular that we are declaring pipe.
slice: It’s a keyword that tells angular that we are using SlicePipe.
start: It is a number type and It’s the starting index to slice the given input(array or string) to return as a subset.
It has the following variation based on the value of it.
  • If the start index is positive value: SlicePipe will return the item at start index and all items after in the list or string expression.
  • If the start index is negative value: SlicePipe will return the item at start index from the end and all items after in the list or string expression.
  • If the start index is positive and greater than the size of the expression:  SlicePipe will return an empty list or string.
  • If the start index is negative and greater than the size of the expression:  SlicePipe will return the entire list or string.

end: It is a number type and It’s the ending index to slice the given input(array or string) to return as a subset.
It also has the following variation based on the value of it.

  • If end index omitted/not provided: SlicePipe will return all items until the end.
  • If end index is positive: SlicePipe will return all items before end index of the list or string.
  • If end index is negative: SlicePipe will return all items before end index from the end of the list or string.

Slice Pipe Example

Array Expression Example

Now we will see an example of SlicePipe using Array Expression.
We’ll take the following array to understand the example.
<br />var arr = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50];<br />

Example 1:
Slice Pipe with positive start and end index

<br />{{arr | slice: 2:5}}<br />

Output

<br />[15,20,25,30]<br />

Here in the above example, we have:
start index:2
end index:5



Example 2
Slice pipe with only positive start index

<br />{{arr | slice: 6}}<br />

Output

<br />[35,40,45,50]<br />


Example 3
Slice pipe with only negative start index

<br />{{arr | slice: -3}}<br />

Output

<br />[40,45,50]<br />


Example 4
Slice pipe with a positive start index and negative end index

<br />{{arr | slice: 3 : -3}}<br />

Output

<br />[20,25,30,35,40]<br />
Our component will look as follows:

<br />import { Component } from '@angular/core';<br />@Component({<br />  selector: 'app-slice-pipe-example',<br />  template: `<br />            &lt;h1&gt;Slice Pipe Example&lt;/h1&gt;<br />            {{arr | slice:2:5}} <br />            {{arr | slice:6}} <br />            {{arr | slice:-3}} <br />            {{arr | slice:3:-3}} <br />        ` <br />})<br />export class SlicePipeExampleComponent {<br />    arr = [10, 15, 20, 25, 30, 35, 40, 45, 50];<br />} <br />

We can use SlicePipe with ngFor as follow:

<br />&lt;div *ngFor="let item of arr | slice:2:5"&gt;<br />&nbsp; {{item}}<br />&lt;/div&gt;&nbsp;<br />

When SlicePipe is used with ngFor, ngFor will be applied to the subset of arr obtained using slice pipe.


String Expression Example


Let’s suppose we are having following string expression.

<br />var str="slice pipe example";<br />


Example 1
Slice pipe with positive start index and end index

<br />{{str | slice:4:6}}<br />

Output

<br />c p<br />

Example 2
Slice pipe with only positive start index

<br />{{str | slice:6}}<br />

Output

<br />pipe example<br />

Example 3
Slice pip with only negative start index

<br />{{str | slice:-3}}<br />

Output

<br />ple<br />

Example 4
Slice pipe with positive start index and negtive end index

<br />{{str | slice:3:-3}}<br />

Output

<br />ce pipe examp<br />

Leave a Comment