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!
In *ngFor the * is a shorthand for using the new angular template syntax with a template tag, this is also called structural Directive.It is helpful to know that * is just a shorthand to explicitly defining the data bindings on a template tag.
The syntax is *ngFor="let <value> of <collection>" . <value> is a variable name of your choosing, <collection> is a property on your component which holds a collection, usually an array but anything that can be iterated over in a for-of loop.
There is no option to break ngFor . You can use a custom pipe that doesn't return values after the element where the condition is met.
Nevermind - this checks if the array is null, not elements within the array.
Instead of the custom pipe, you could just do a null check right in the *ngFor, I think it's pretty readable. You're just providing an empty array if performances is null:
<tr *ngFor="let performance of (performances ? performances : [])"> 
                        UPDATED on 03.01.2020
There are multiple ways.
Using only the template:
<tbody>     <tr *ngFor="let performance of (performances || [])">       <td>{{ performance.id }}</td>       ...     </tr> </tbody>   <tbody>     <tr *ngFor="let performance of performances">       <td>{{ performance?.id || 1337 }}</td>       ...     </tr> </tbody>   <tbody>     <tr *ngFor="let performance of performances">       <td *ngIf="performance?.id; else elsePerfId">{{ performance.id }}</td>       <ng-template #elsePerfId>         <td>default</td>       </ng-template>       ...     </tr> </tbody>   <tbody>     <tr *ngFor="let performance of (performances | defaultValueIfNullPipe)">       <td>{{ performance.id }}</td>       ...     </tr> </tbody>   Using Component..
You could also take care that there are no invalid values:
getFunds(){     this._service.getData().then(         performances => this.performances = performances || [] /* empty array or default value here */     ); } 
                        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