const books: Array<{ id: number, title: string }> = [{id: 1, title: "book1"}, {id: 2, title: "book2"}, {id: 3, title: "book3"}, {id: 4, title: "book4"}];
<form [formGroup]="bookForm" novalidate (ngSubmit)="save(bookForm)">
<div class="form-group">
<label>book_id</label>
<input list="book_list" formControlName="book_id">
<datalist id="book_list">
<option *ngFor="let book of books" [ngValue]="book.id">{{book.title}}</option>
</datalist>{{ bookForm.value | json }}
<!--display error message if book_id is not valid-->
<small [hidden]="bookForm.controls.book_id.valid">
book not selected</small>
</div>
</form>
I am trying to do autocomplete input field using input datalist. Here i want to allow search by book title but want to get book id once one book is selected in angular 2.
that should be [value]
not [ngvalue]
In component you can extract like below:
save(form) {
console.log(form.book_id.value);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.