我在我的Angular 2应用程序中构建了一个搜索框,试图在按'Enter'(表单不包含按钮)时提交该值。在Angular 2中,如何在输入时提交input type =“text”的值?
模板
<input type="text" id="search" class="form-control search-input" name="search" placeholder="Search..." [(ngModel)]="query" (ngSubmit)="this.onSubmit(query)">
随着用于测试目的的简单的onsubmit功能...
export class HeaderComponent implements OnInit {
constructor() {}
onSubmit(value: string): void {
alert('Submitted value: ' + value);
}
}
ngSubmit似乎并没有在这种情况下工作。那么解决这个问题的'Angular 2 way'是什么? (最好不喜欢隐藏按钮黑客)
不错!我使用'
'来包装它,但是你的方法更简洁明了! –整洁。谢谢 – user2180794
这也在[Angular Docs]中提到(https://angular.io/docs/ts/latest/guide/user-input.html#!#key-event-filtering-with-key-enter-) 。 –