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>
我正在尝试使用输入datalist进行自动完成输入字段。在这里,我想允许按书名搜索,但是想要在角度2中选择一本书后获得书籍。
答案 0 :(得分:0)
应为[value]
而不是[ngvalue]
在组件中,您可以像下面这样提取:
save(form) {
console.log(form.book_id.value);
}