Components: Autocomplete formcontrol valueChanges fired when user select option

Created on 19 Jan 2019  路  5Comments  路  Source: angular/components

What is the expected behavior?

Autocomplete formcontrol valueChanges should not be fired when user select option

What is the current behavior?

Autocomplete formcontrol valueChanges fired when user select option

What are the steps to reproduce?

Please, look at this (open console): https://stackblitz.com/edit/angular-8zucqs?file=app/autocomplete-display-example.ts

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular - 7.2.0, Material - 7.2.1

Is there anything else we should know?

Bros, i need this functionality.

materiaautocomplete

Most helpful comment

If you look at an example to autocomplete component, you will see something like this:

constructor() {
    this.filteredStates = this.stateCtrl.valueChanges
      .pipe(
        startWith(''),
        map(state => state ? this._filterStates(state) : this.states.slice())
      );
  }

I'm as a regular developer, think that is a place where I can make a call to API. So I place code into map pipe. If I type something in input, that call will be fired (it is OK), but when I select an option, that call will be fired again (valueChanges). So if it isn't an issue, please, give me example how to avoid it.

All 5 comments

I'm not sure I get what the issue is. When is it supposed to fire valueChange event, aside from when the value has changed?

If you look at an example to autocomplete component, you will see something like this:

constructor() {
    this.filteredStates = this.stateCtrl.valueChanges
      .pipe(
        startWith(''),
        map(state => state ? this._filterStates(state) : this.states.slice())
      );
  }

I'm as a regular developer, think that is a place where I can make a call to API. So I place code into map pipe. If I type something in input, that call will be fired (it is OK), but when I select an option, that call will be fired again (valueChanges). So if it isn't an issue, please, give me example how to avoid it.

If you look at an example to autocomplete component, you will see something like this:

constructor() {
    this.filteredStates = this.stateCtrl.valueChanges
      .pipe(
        startWith(''),
        map(state => state ? this._filterStates(state) : this.states.slice())
      );
  }

I'm as a regular developer, think that is a place where I can make a call to API. So I place code into map pipe. If I type something in input, that call will be fired (it is OK), but when I select an option, that call will be fired again (valueChanges). So if it isn't an issue, please, give me example how to avoid it.

When you select option, valueChanges fired with [Object], try filter

constructor() {
    this.filteredStates = this.stateCtrl.valueChanges
      .pipe(
        startWith(''),
        filter(x => typeof x === 'string'),
        map(state => state ? this._filterStates(state) : this.states.slice())
      );
  }

Closing since this works as expected.

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Miiekeee picture Miiekeee  路  3Comments

RoxKilly picture RoxKilly  路  3Comments

3mp3ri0r picture 3mp3ri0r  路  3Comments

constantinlucian picture constantinlucian  路  3Comments

xtianus79 picture xtianus79  路  3Comments