16

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

3 Answers 3

34

Use FormControl SetValue method to set Default Value

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

@Chellappan, how about passing different value and text ?
you mean different key value pair?
The stackblitz doesn't appear to be setting a default value
@brt it is working fine in stackblitz
6

set Initial value of FormControl

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

Comments

1

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

2 Comments

Thank for you help.
This example is not filtering the values on typing the values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.