Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Pipe under condition

Is it possible in Angular 2 to apply a pipe under condition? I would like to do something like:

{{ variable.text | (variable.value ? SomePipe : OtherPipe) }} 

If not, what is the preferred way to achieve this effect?

like image 750
Daniel Kucal Avatar asked Apr 29 '16 02:04

Daniel Kucal


2 Answers

You need to change the syntax a bit:

{{variable.value ? (variable.text | SomePipe) : (variable.text | pipe2)}} 

Plunker example

like image 89
Günter Zöchbauer Avatar answered Sep 25 '22 20:09

Günter Zöchbauer


You could also use ngIf

<ng-container *ngIf="variable.value; else elseBlock">{{ variable.text | SomePipe }}</ng-container> <ng-template #elseBlock>{{ variable.text | OtherPipe }}</ng-template> 

I find it useful in case the line becomes too long.

like image 36
ajorquera Avatar answered Sep 24 '22 20:09

ajorquera