che*_*thu 2 angular-material angular
我有两个下拉列表,其中填充了来自服务器的数据。第一个下拉列表包含州,第二个包含城市。就像我选择一个州名一样,只有该州的城市应该使用 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>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序组件.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>
Run Code Online (Sandbox Code Playgroud)
在您的情况下,您需要从上面的示例中进行更改,即使用服务器中的数据填充国家/城市地图。
| 归档时间: |
|
| 查看次数: |
9853 次 |
| 最近记录: |