0

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. :)

8
  • I suppose that when you press enter you are triggering the form submit. Have you checked that? Commented Jun 28, 2017 at 7:57
  • You are not giving us enough code. Please provide the whole form and whole submit method Commented Jun 28, 2017 at 8:03
  • please update the code with the whole <form> not just the <input> Commented Jun 28, 2017 at 8:21
  • @crash I have not used <form> element, just <input> element inside div tag. Commented Jun 28, 2017 at 8:23
  • @FredrikLundin I have not used form element. Just input inside div tag. And submit() is with whole code. :) Commented Jun 28, 2017 at 8:28

2 Answers 2

1

Adding event.preventDefault() in the onEnter function should solve the problem since it is maybe trying to send a non-existing form.

Sign up to request clarification or add additional context in comments.

Comments

0

change the name of function onEnter() from your template and your component. use any other name not this.

1 Comment

i think there might be any onEnter events in html or js. not sure about this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.