Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - How can I write a condition in interpolation?

I have a table which is being populated through the add client form. It works fine and the changes are displayed. The thing is I have a select list in which a user selects the specific source and then it is saved in ngmodel. Here is the code.

Select List

 <mat-select [(ngModel)]="model.source" name="source" placeholder="Select Source ">       <mat-option [value]="1 ">Upwork</mat-option>       <mat-option [value]="2 ">Refer from Friend</mat-option>       <mat-option [value]="3 ">Other</mat-option>     </mat-select> 

Now in my table the value which displays is 1 or 2 or 3 based on the selection. I want to write something, a condition in interpolation like this.

Table Field

<ng-container matColumnDef="source">   <mat-header-cell *matHeaderCellDef> Source </mat-header-cell>   <mat-cell *matCellDef="let element"> {{element.source ? if value is 1 : display (upwork), if value is 2 : display(refer from friend)}} </mat-cell> </ng-container> 

Some thing like this, I did like it in angularjs I am not sure about Angular

like image 849
Usman Iqbal Avatar asked Nov 23 '17 13:11

Usman Iqbal


People also ask

What is the syntax for interpolation in angular?

Displaying values with interpolationlink Interpolation refers to embedding expressions into marked up text. By default, interpolation uses the double curly braces {{ and }} as delimiters. Angular replaces currentCustomer with the string value of the corresponding component property. In this case, the value is Maria .

Can we use expressions Inside string interpolation in angular?

The angular evaluates the expressions into a string and replaces it in the original string and updates the view. You can use interpolation wherever you use a string literal in the view. Angular interpolation is also known by the name string interpolation. Because you incorporate expressions inside another string.

When using angular interpolation a property can be?

Angular interpolation is used display a component property in the respective view template with double curly braces syntax. We can display all kind of properties data into view e.g. string, number, date, arrays, list or map. Data binding consist of one way data binding and two way data binding.

How do I add spaces to a string interpolation?

You can use string interpolation by using template literals; replace the quotes with backticks(`)(https://www.computerhope.com/jargon/b/backquot.htm). Hope this helped you on your coding journey! I am not sure why they are teaching this method, but you can just add a space between the quotation marks.


1 Answers

You can use nested ternary if

{{element.source == 1 ? 'upwork' : (element.source == 2 ? 'refer from friend' : '')}} 

or probably better

export class MyComponent {   sourceNames = {1: 'upwork', 2: 'refer from friend', 3: 'other' }; } 
{{sourceNames[element.source]}} 
like image 113
Günter Zöchbauer Avatar answered Sep 16 '22 21:09

Günter Zöchbauer