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
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 .
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.
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.
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.
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]}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With