I want to access array inside objects which is coming from my json file to populate my dropdown values and want to show values on second dropdown only from previous selected dropdown.
here is my ts:
companySites = false;
siteSelected = false;
alldata: any = [];
getCustomerData() {
this.maintenanceServices.getAllCustomers().subscribe(
data => {
if (data) {
this.alldata = data;
console.log(this.alldata);
}
},
error => {
console.log(error);
}
);
}
onCustomerChange(): void {
this.companySites = true;
}
onSitecChange(): void {
this.siteSelected = true;
}
.html:
<mat-grid-tile>
<div>
<p>Select a company</p>
<mat-form-field>
<mat-select placeholder="Company" (selectionChange)="onCustomerChange($event)" value="company">
<mat-option *ngFor="let company of alldata">
{{ company.customer.customerName }}
</mat-option>
</mat-select>
</mat-form-field>
</div>
</mat-grid-tile>
<mat-grid-tile *ngIf="companySites">
<div>
<p>Select site</p>
<mat-form-field>
<mat-select placeholder="Sites" (selectionChange)="onSitecChange($event)" value="site">
<mat-option *ngFor="let site of alldata.sites; index as i">
{{ site.buildingName }}
</mat-option>
</mat-select>
</mat-form-field>
</div>
</mat-grid-tile>
</mat-grid-list>
and json:
"customer": {
"customerName": "ABC",
"customerAddress": "NYC",
"BillingAddress": "ABC",
"ProductInstallation": "drive A",
"sites": [
{
"_id": "5c5be1f2b73bc81dd404ac00",
"buildingName": "Main Building",
"requestDate": "12/02/2019",
"buildingAddress": "123 Street",
"numberofDrives": "3"
},
{
"_id": "5c5be1f2b73bc81dd404abff",
"buildingName": "second Building",
"requestDate": "12/02/2019",
"buildingAddress": "123 Street",
"numberofDrives": "2"
}
]
},
"_id": "5c5be1f2b73bc81dd404abfe",
now my json has hundreds of customers with multiple sites. I want to select company and then on second dropdown I want to show sites for selected sites
I have tried:
Object.values but that didn't work either