1

I am calling service in Angular7 at every dropdown option change. But when I change selected option on dropdown I am getting getAllByCountryId of undefined error.

Here is function that calling http service:

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;
      });
  }

Here is the HTML code that calling above function on every dropdown option change:

<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) and countryOnChange is connected at the backend.

Here is how to call countryOnChange function:

const dropdown2 = new InputDropdown({
      key: 'countryId',
      label: '',
      onChange: this.countryOnChange,
      options: this.countries,
      value: '',
      required: true,
      order: 3
    });

Error http://prntscr.com/ovjxe7

How can I solve this problem?

2 Answers2

0

I think [ngModel] is needed:

  <select [ngModel]="selectedValue" (ngModelChange)="onDropdownChange($event)" 
   class="form-control">
            <option *ngFor="let category of categorylist" [ngValue]="category.id"> 
         {{category.Description}}</option>
   </select>  

refer to: Angular 2 How to bind selected id in Dropdownlist with model?

Micha
  • 906
  • 6
  • 9
0

getAllByCountryId of undefined error means you are getting this.cityService as undefined. Check with which this context your function is binding when you are creating the InputDropDown. May be you need to define it like :-

const dropdown2 = new InputDropdown({
      key: 'countryId',
      label: '',
      onChange: this.countryOnChange.bind(this),
      options: this.countries.bind(this),
      value: '',
      required: true,
      order: 3
});

Hope this helps.

Sanket
  • 612
  • 5
  • 23
  • Why do you bind `options: this.countries.bind(this),` ? – robert Aug 21 '19 at 12:05
  • If you want to change countries then you can bind otherwise ignore binding countries. Did it worked with countryOnChange ? – Sanket Aug 21 '19 at 12:40
  • According to the latest comment under the question it did. – robert Aug 21 '19 at 12:43
  • ohh , i didn't checked the comments. Good to know i was helping correctly. – Sanket Aug 21 '19 at 13:05
  • There is another problem here. Yes, countryOnChange can be captured but this trigger should populate another dropdown(cities) and this dropdown is like country above too. So how to populate another dropdown after countryOnChange triggered. I tried this: `this.formComponent.form.controls['cityId'].patchValue(this.cities);` But didn't work. @robert, @Sanket – Mümin Celal Pinar Aug 22 '19 at 09:31
  • I solved. I used: `((this.formComponent.inputs as InputBase[]).find(i => i.key === 'cityId') as InputDropdown).options = options;` – Mümin Celal Pinar Aug 22 '19 at 10:15