问题
I have two dropdowns that I populate with data from the server. The first dropdown contains a state, and the second one contains city. Like if I select one state name only cities in that state should show in 2nd drop down using Angular 4.
<mat-tab-group>
<mat-tab label="Basic Info">
<ng-template mat-tab-label>
<div fxLayout="center center">
<mat-icon style="font-size: 16px; line-height: 18px">gps_fixed</mat-icon>
State
</div>
</ng-template><br /><br /><br /><br />
<div class="ui fluid container" fusePerfectScrollbar>
<div class="content">
<mat-form-field>
<mat-select placeholder="State" [(ngModel)]="selectedValue" name="state" (change)="dropdownchange($event.target.value)">
<mat-option>None</mat-option>
<mat-option *ngFor="let state of states" [value]="state">{{state}}</mat-option>
</mat-select>
</mat-form-field><br />
<h3>You selected: {{selectedValue}}</h3>
</div>
</div>
</mat-tab>
<mat-tab label="Location">
<ng-template mat-tab-label>
<div fxLayout="center center">
<mat-icon style="font-size: 16px; line-height: 18px">location_on</mat-icon>
City
</div>
</ng-template>
<div class="ui fluid container" fusePerfectScrollbar>
<div class="ui fluid container" fusePerfectScrollbar>
<div class="content">
<mat-form-field>
<mat-select placeholder="City">
<mat-option>None</mat-option>
<mat-option *ngFor="let cities of city" [value]="state">{{cities}}</mat-option>
</mat-select>
</mat-form-field><br />
</div>
</div>
</div>
</mat-tab>
</mat-tab-group>
回答1:
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
private map = new Map<string, string[]>([
['Poland', ['Warszawa', 'Krakow']],
['USA', ['New York', 'Austin']],
])
country: string;
city: string;
get countries(): string[] {
return Array.from(this.map.keys());
}
get cities(): string[] | undefined {
return this.map.get(this.country);
}
}
app.component.html
<select [(ngModel)]="country">
<option *ngFor="let country of countries" [value]="country"> {{ country }} </option>
</select>
<select *ngIf="country" [(ngModel)]="city" [value]="city">
<option *ngFor="let city of cities"> {{ city }} </option>
</select>
Live demo
In your case all you need to change from the example above is to populate the country/cities map with the data from your server.
回答2:
You should add an click event into your State's dropbox like
<mat-option *ngFor="let state of states" (click)="getCities(state)" [value]="state">{{state}}</mat-option>
Then add a function like getCities(state:string) into your .ts file and fill the second dropbox content with this function like
public cities : string[];
getCities(state:string) {
this.cities = []; // empty the array first.
this.cities.push(CITY LIST);
}
In this way as soon as a state selected , it's cities will load inside the second dropbox.
Hope it helps.
来源:https://stackoverflow.com/questions/49771116/select-second-dropdown-based-on-first-dropdown-using-angular-4