我正在使用angular 2 select2下拉菜单,下面是HTML:
<select2 [data]="countries" [value]="countrySelected" (valueChanged)="onSelectCountry($event)"></select2>
<select2 [data]="regions" [value]="regionSelected" (valueChanged)="onSelectRegion($event)"></select2>
<select2 [data]="cities" [value]="citySelected" (valueChanged)="onSelectCity($event)"></select2>
我要做的是填充数据并设置默认值,首先,我要获取国家/地区的数据并设置默认值:
ngOnInit() {
this.userService.profile().subscribe(data=>{
this.user = data;
this.locationService.getCountrys('all').subscribe(data => {
this.countries = data;
this.countrySelected = this.user.location.country.id;
});
});
}
这会触发onSelectCountry事件:
onSelectCountry($value){
this.locationService.getRegions($value.value).subscribe(data => {
this.regions = data;
this.regionSelected = this.user.location.region.id;
});
}
然后我根据国家/地区获取区域并设置城市的默认值,然后触发onSelectRegion:
onSelectRegion($value){
this.locationService.getCities($value.value).subscribe(data => {
this.cities = data;
this.citySelected = this.user.location.city.id;
});
}
根据地区获取城市并设置地区默认值
我的问题是根本没有设置默认值,填充了数据,但是它们转到第一个值。我在做什么错了?