I would like to fetch data from server using Autocomplete Component with angular2 / material2. (https://material.angular.io/components/component/autocomplete)
ts
emailCtrl: FormControl;
filteredEmails: any;
constructor(
private companieService: CompanieService,
) {
this.emailCtrl = new FormControl();
this.filteredEmails = this.emailCtrl.valueChanges
.startWith(null)
.map(email => this.filterEmails(email));
}
filterEmails(email: string) {
this.userService.getUsersByEmail(email)
.subscribe(
res => {
return res
},
error => {
console.log(error);
}
)
}
html
<md-input-container>
<input mdInput placeholder="Email" [mdAutocomplete]="auto" [formControl]="emailCtrl" [(ngModel)]="fetchedUser.profile.email">
</md-input-container>
<md-autocomplete #auto="mdAutocomplete">
<md-option *ngFor="let email of filteredEmails | async" [value]="email">
{{email}}
</md-option>
</md-autocomplete>
Service: userService.getUsersByEmail(email)
is pulling this kind of data:
['email1@email.com','email2@email.com','email3@email.com']
I have no errors but no results in the autocomplete. I see in debugger of chrome (tab network) Data are pulled correctly for each changes in the input