角度2:输入错误(Angular 2 : Input error)

编程入门 行业动态 更新时间:2024-10-20 21:00:09
角度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 connected

I'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.

更多推荐

本文发布于:2023-08-05 07:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428844.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:角度   错误   Angular   Input   error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!