I'm showing some records coming from an api and some of the results are null and I would like to show a different value in case the response values are null. How would be the best way to do it?
Below is my code:
Component:
import {Component, OnInit} from '@angular/core';
import {PerformancesService} from '../../services/performances.service';
import {Performance} from '../../common/performance';
@Component({
selector: 'performances',
templateUrl: './app/components/performances/performances.component.html'
})
export class PerformancesComponent implements OnInit{
performances: Performance[];
constructor(private _service : PerformancesService){
}
getFunds(){
this._service.getData().then(
performances => this.performances = performances
)
}
ngOnInit(){
this.getFunds();
}
}
template:
<h2>Performance</h2>
<table class="table table-hover">
<thead>
<tr>
<th>Fund Id</th>
<th>Country Id</th>
<th>1Mth</th>
<th>3Mth</th>
<th>YTD</th>
<th>1Yr</th>
<th>3Yrs</th>
<th>5Yrs</th>
<th>10Yrs</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let performance of performances">
<td>{{performance.id}}</td>
<td>{{performance.country_id}}</td>
<td>{{performance.OneMonthBack}}</td>
<td>{{performance.ThreeMonthsBack}}</td>
<td>{{performance.YearToDate}}</td>
<td>{{performance.OneYearBack}}</td>
<td>{{performance.ThreeYearsBack}}</td>
<td>{{performance.FiveYearsBack}}</td>
<td>{{performance.TenYearsBack}}</td>
</tr>
</tbody>
</table>
I don't know if I could do it in the template or I should check each value in my component. Any idea? Thanks!
getDefaultIfNullthat takes any value as argument and returns actual value if not null and returns"-"if null. Using it like this -{{getDefaultIfNull(performance.OneMonthBack)}}