角度2:输入错误(Angular 2 : Input error)
我正在开发一个已经实现搜索功能的项目。 input元素如下所示:
<input #input type="text" name="query" class="form-control" id="nav-input" (ngModelChange)="onquery($event)" [(ngModel)]="searchdata.query" autocomplete="off" (keypress)="onEnter($event)"> onEnter(event: any) { if (event.which === 13) { this.displayStatus = 'hidebox'; event.target.blur(); this.submit(); } submit() { if (this.searchdata.query.toString().length !== 0) { if (!this.router.url.toString().includes('/search')) { this.router.navigate(['/search'], {queryParams: this.searchdata}); }在键入查询并按Enter键时,它在控制台中显示错误:
Form submission canceled because the form is not connected我无法弄明白,如何解决这个问题。 如果有人可以帮助我,那将是很棒的。 :)
I'm working on a project in which a search feature has been implemented. The input element looks like this :
<input #input type="text" name="query" class="form-control" id="nav-input" (ngModelChange)="onquery($event)" [(ngModel)]="searchdata.query" autocomplete="off" (keypress)="onEnter($event)"> onEnter(event: any) { if (event.which === 13) { this.displayStatus = 'hidebox'; event.target.blur(); this.submit(); } submit() { if (this.searchdata.query.toString().length !== 0) { if (!this.router.url.toString().includes('/search')) { this.router.navigate(['/search'], {queryParams: this.searchdata}); }On typing a query and hitting enter key, it shows error in console:
Form submission canceled because the form is not connectedI'm not able to figure out, how to solve this issue. It will be great if someone can help me out. :)
最满意答案
在onEnter函数中添加event.preventDefault()应该可以解决问题,因为它可能正在尝试发送不存在的表单。
Adding event.preventDefault() in the onEnter function should solve the problem since it is maybe trying to send a non-existing form.
更多推荐
发布评论