Creates a new List or String containing a subset (slice) of the elements.

Usage

expression | slice:start[:end]

The input expression must be a List or String.

  • start: the starting index of the subset to return.
    • a positive integer: return the item at start index and all items after in the list or string expression.
    • a negative integer: return the item at start index from the end and all items after in the list or string expression.
    • if positive and greater than the size of the expression: return an empty list or string.
    • if negative and greater than the size of the expression: return entire list or string.
  • end: The ending index of the subset to return.
    • omitted: return all items until the end.
    • if positive: return all items before end index of the list or string.
    • if negative: return all items before end index from the end of the list or string.

When operating on a List, the returned list is always a copy even when all the elements are being returned.

Examples

<ul>
    <li *ngFor="let i of ['a', 'b', 'c', 'd'] | slice:1:3">{{i}}</li>
</ul>

<pre>
  {{str}}[0:4]:   '{{str | slice:0:4}}' --> 'abcd'
  {{str}}[4:0]:   '{{str | slice:4:0}}' --> ''
  {{str}}[-4]:    '{{str | slice:-4}}' --> 'ghij'
  {{str}}[-4:-2]: '{{str | slice:-4:-2}}' --> 'gh'
  {{str}}[-100]:  '{{str | slice:-100}}' --> 'abcdefghij'
  {{str}}[100]:   '{{str | slice:100}}' --> ''
</pre>

The first example generates two <li> elements with text b and c. The second example uses the string 'abcdefghij'.

Implements
Annotations
  • @Pipe('slice', pure: false)

Constructors

SlicePipe()

Properties

hashCode → int
The hash code for this object. [...]
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited

Methods

supports(obj) → bool
transform(value, num start, [ num end ]) → dynamic
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() → String
Returns a string representation of this object.
inherited

Operators

operator ==(other) → bool
The equality operator. [...]
inherited