CommonSenseCode
CommonSenseCode

Reputation: 25389

Angular2 Use sort pipe only when certain value or boolean is true

My markup looks like this:

 <li *ngFor="let user of usersList | orderBy : ['name']; let i = index;">

So is there anyway to apply orderBy pipe only when certain boolean is true?

Upvotes: 5

Views: 1682

Answers (2)

Kevin Le
Kevin Le

Reputation: 856

How about something like

<li *ngFor="let user of usersList | orderBy : [value ? 'name' : '']; let i = index;">

Another option would be creating a wrapper pipe that takes a parameter and uses the orderBy pipe conditionally.

*EDIT

As @OliverRenner states, orderBy is no longer offered by Angular in 2.0. The above assumes that you have written your own orderBy implementation. See https://angular.io/docs/ts/latest/guide/pipes.html#!#no-filter-pipe

Upvotes: 3

Oliver Renner
Oliver Renner

Reputation: 563

since orderBy does not exist in angular2, you need a custom pipe anyways (if you want to use a pipe at all) .. therefore you can pass the boolean into this pipe and only apply sorting when true.

Upvotes: 0

Related Questions