I have an project api that either contains material_projects or project_services. I am confused on how would i display either one of them. If i display material_projects then i mustn't display project_services. project_services must be an empty in the api.
getAllProjects() {
this.subscription = this.projectsService.getAll()
.subscribe(
(data:any) => {
this.projects = data.projects;
console.log(data);
},
error => {
console.log(error);
});
}
html
<div class="card-block" *ngFor="let project of projects | search : searchBOM">
<h2 class="proj-name">{{ project.name | titlecase }} </h2>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Material SKU</th>
<th>Material Name</th>
<th>Unit</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let innerItem of project.material_projects">
<td>{{innerItem.material.sku}}</td>
<td>{{innerItem.material.name}}</td>
<td>{{innerItem.unit}}</td>
</tr>
</tbody>
</table>
</div>
If you can guarantee that constraint, then you may be able to simply iterate over both lists.
As an alternative, you may conditionally display one or the other:
Or, if the DTOs are similar enough, you may consider sharing more of the view logic:
EDIT:
If you would like to use different tables based on the existence of properties, then you will want to move your
*ngIf
statements up to the<table>
element or an immediate child. Based on your comments, you might try something like the following:Component code:
There are many ways to do this. Another option would be to use a switch statement in your template, or adding additional child ("dummy") components to handle the behavior.