每次下拉选项更改时,我都会调用 Angular7 中的服务。但是,当我更改下拉列表中的选定选项时,我收到 getAllByCountryId of undefined
错误。
这里是调用http服务的函数:
countryOnChange(countryId: Guid) {
this.cityService.getAllByCountryId(countryId).subscribe(
res => {
if (res.code === ResponseCode.success) {
res.dataList.map(item => {
this.cities.push({
value: item.id.toString(),
label: item.dataValue
});
});
if (this.formComponent !== undefined) {
this.formComponent.form.controls['cityId'].patchValue(this.cities);
}
}
},
err => {
this.error = true;
});
}
以下是在每次下拉选项更改时调用上述函数的 HTML 代码:
<ng-container *ngSwitchCase="'dropdown'" class="col-md-12">
<ng-select [ngClass]="'ng-select'" [options]="input.options" [formControlName]="input.key" [id]="input.key" (selected)="input.onChange($event.value)"></ng-select>
</ng-container>
input.onChange($event.value)
和 countryOnChange
在后端连接。
以下是调用countryOnChange函数的方法:
const dropdown2 = new InputDropdown({
key: 'countryId',
label: '',
onChange: this.countryOnChange,
options: this.countries,
value: '',
required: true,
order: 3
});
Error http://prntscr.com/ovjxe7
如何解决这个问题?
最佳答案
我认为需要 [ngModel]:
<select [ngModel]="selectedValue" (ngModelChange)="onDropdownChange($event)"
class="form-control">
<option *ngFor="let category of categorylist" [ngValue]="category.id">
{{category.Description}}</option>
</select>
引用:Angular 2 How to bind selected id in Dropdownlist with model?
关于javascript - 如何解决服务 "of undefined"问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57590294/