我正在我的Angular 2应用程序中构建一个搜索框,尝试在点击"Enter"时提交值(表单不包含按钮).
模板
<input type="text" id="search" class="form-control search-input" name="search" placeholder="Search..." [(ngModel)]="query" (ngSubmit)="this.onSubmit(query)">
Run Code Online (Sandbox Code Playgroud)
使用简单的onSubmit函数进行测试......
export class HeaderComponent implements OnInit {
constructor() {}
onSubmit(value: string): void {
alert('Submitted value: ' + value);
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,ngSubmit似乎不起作用.那么解决这个问题的'Angular 2方式'是什么?(最好没有像隐藏按钮这样的黑客)
God*_*her 100
(keyup.enter)="methodInsideYourComponent()"
在输入标记内添加此内容
归档时间: |
|
查看次数: |
26482 次 |
最近记录: |