PGH
PGH

Reputation: 2204

To make default selection in autocomplete (material)

I am using autocomplete component(i,e Display value autocomplete) for my project.Here is the stackblitz example

How can i set a any one list item as default ? like this enter image description here

Upvotes: 16

Views: 31830

Answers (3)

user4676340
user4676340

Reputation:

Use RxJs' tap operator : stackblitz

ngOnInit() {
    this.filteredOptions = this.myControl.valueChanges
      .pipe(
        startWith<string | User>(''),
        map(value => typeof value === 'string' ? value : value.name),
        map(name => name ? this._filter(name) : this.options.slice()),
        tap(() => this.myControl.setValue(this.options[0]))
      );
  }

Upvotes: 1

Krishna Rathore
Krishna Rathore

Reputation: 9687

set Initial value of FormControl

myControl = new FormControl({name: 'Shelley'});

Upvotes: 6

Chellappan வ
Chellappan வ

Reputation: 27303

Use FormControl SetValue method to set Default Value

this.myControl.setValue( {name: 'Mary'});

Example:https://stackblitz.com/edit/angular-8r153h

Upvotes: 34

Related Questions