I am trying to make autocomplete form with angular.
My component.html page
<form method="post" [formGroup]="uploadForm" >
<div class="form-group">
<label for="sem">Select Semester</label>
<input type="text" class="form-control" [(ngModel)]="model" [ngbTypeahead]="search" formControlName="branch" id="sem" placeholder="Branch Name">
</div>
<button type="submit" [disabled]="" class="btn btn-primary">Submit</button>
</form>
and my component.ts page which i get from typeahead examples
uploadForm: FormGroup;
constructor(private formBuilder: FormBuilder, private userService: UserService) { }
ngOnInit() {
this.search = (text$: Observable<string>) => text$.pipe(
distinctUntilChanged(),
map(term => term.length < 2 ? []
: this.result.filter(v => v.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10))
);
}
and my user.service.ts is
branchData(branchData) {
return this.http.post<DataResponse>('/user/branchSearch', branchData);
}
The data will come from server, so for every word type in input field , query will be send to the server that will return the array something like this
[
{
_id: "1",
branch: "A"
},
{
_id: "2",
branch: "B"
},
]
and that branch will shows on dropdown.
So how will I send every typed word query to server and then show the result in dropdown.
https://ng-bootstrap.github.io/#/components/typeahead/examples