javascript - 如何解决服务 "of undefined"问题?

标签 javascript angular typescript rest api

每次下拉选项更改时,我都会调用 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/

相关文章:

javascript - 哪些操作触发 'Update layer tree' ?

javascript - 如何为Angular 5中的函数调用提供时间延迟?

javascript - Angular javascript 页面编译但显示空白

typescript - Angular 2 中的 angular.isUndefined 和 angular.equals

javascript - 从 Firebase RTDB 和存储获取数据

javascript - "#"keyCode = 222 与 51(Chrome 与 Android 版 Chrome)- 为什么不同?

java - 在 Spring Boot Angular 8 Web 应用程序中验证和管理用户的最简单方法是什么?

node.js - 在 Typescript 中增加全局范围不起作用

javascript - 是否可以在窗口关闭时删除 localstorage 项目?

html - 悬停时无法显示按钮