Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 - Enter Key executes first (click) function present on the form

I don't know why each time I press ENTER key in an input element the application execute the first function it finds in the html .

How Can I prevent it ?

I tried to make some test with (keyup.enter) event but it first execute the first (click) function, then the the (keyup.enter) function...

This is the code:

html form

<form role="form" [formGroup]="FormOffertDetail" (keyup.enter)="checkKey()">

<p-dialog modal="modal" [(visible)]="displayModal_Duplicate" width="300" responsive="true">
  <p-header>
    Dupplica Offerta
  </p-header>

  <div class="form-group">
    <label for="idCustomer">Selezionare il Cliente</label>
    <!--<input formControlName="idCustomer" type="text" class="form-control input-sm" id="idCustomer" placeholder="" required autofocus>-->
    <select id="idCustomer_dup" class="form-control" formControlName="idCustomer_dup" [(ngModel)]="idCustomer_dup">
                    <option [value]="c.idCustomer" *ngFor="let c of listCustomers">
                      {{c.businessName}}
                    </option>
                    </select>
  </div>

  <p-footer>
    <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
      <button class="btn btn-primary btn-space" (click)="do_OffertDuplicate()"><i class="fa fa-copy fa-lg" aria-hidden="true"></i> Dupplica</button>
      <button class="btn btn-danger btn-space" (click)="displayModal_Duplicate=false"><i class="fa fa-trash-o fa-lg" aria-hidden="true"></i> Annulla</button>
    </div>
  </p-footer>
</p-dialog>

......

component.ts functions

 do_OffertDuplicate() {
    if (this.idCustomer_dup == null) {
      alert("Selezionare il Cliente");
      return;
    }

    this.offertService.Duplicate(0, this.offert.idOffert, this.idCustomer_dup).subscribe(
      res => {
        this.displayModal_Duplicate = false;
        alert("OFFERTA DUPPLICATA ( "+res.idOffert+" )");
        this.router.navigate(['OffertDetail', res.idOffert]);
        
      }
    );

  }

checkKey()
{
  alert();
}

Thanks to support!

like image 832
DarioN1 Avatar asked Apr 27 '17 15:04

DarioN1


Video Answer


1 Answers

Without a type on the button, it is seen as a submit button so it may be attempting to submit the form. Try adding the type to the button tag:

<button
  ...
  type="button"
  ...
>
like image 104
DeborahK Avatar answered Oct 04 '22 00:10

DeborahK