Reputation: 2204
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
Upvotes: 16
Views: 31830
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
Reputation: 9687
set Initial value of FormControl
myControl = new FormControl({name: 'Shelley'});
Upvotes: 6
Reputation: 27303
Use FormControl SetValue method to set Default Value
this.myControl.setValue( {name: 'Mary'});
Example:https://stackblitz.com/edit/angular-8r153h
Upvotes: 34