angular – prevent default submit on click of a button

I had a form with ngSubmit

<form name="editForm" role="form" novalidate (ngSubmit)="save()" #editForm="ngForm">

and inside this form I have 3 buttons like so

<div class="btn-group flex-btn-group-container">

            <span class="fa fa-eye"></span>
            <span class="hidden-md-down">Descargar</span>


            <span class="fa fa-pencil"></span>
            <span class="hidden-md-down">Edit</span>


            <span class="fa fa-remove"></span>
            <span class="hidden-md-down">Delete</span>

</div>

two are of type submit and one has a click handler.

Whenever I clicked the first button (click handler), it was submitting the form. To prevent this you should add the $event parameter

<button (click) = "descargarAdjunto(adjunto, $event)" class="btn btn-info btn-sm">
    <span class="fa fa-eye"></span>
    <span class="hidden-md-down">Descargar</span>
</button>

and call the prevent default

private descargarAdjunto(adjunto, $event){
        $event.preventDefault()
        ...
}

fuentes:
https://coderwall.com/p/hvy1rg/angularjs-how-to-prevent-form-submission-after-click-on-button
https://github.com/angular/angular.js/issues/6017
https://docs.angularjs.org/api/ng/directive/form

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

w

Conectando a %s