Matoy Matoy - 1 year ago 65
HTML Question

angular ng-if automaticly sends a form when true

I have a angular js form and a button that, if pressed, shows the login fields (2 inputs for username and password, and 1 submit button) and another button that hides it.

I've written this simple code below that uses the

directive. it is working fine only problem that after the use press on show login, the div is being shown but the form is being submitted automaticly (without the user press on the submit button).

someone knows why this is happening and how to fix it?


<form name="loginForm" width="70%">

<button ng-click="NewConversation=true" >show login</button>
<button ng-click="NewConversation=false" >hide login</button>
<div ng-if="NewConversation">
<input autocomplete="off" name="name" ng-model="name" required placeholder="Name">
<input autocomplete="off" name="room" ng-model="room" required placeholder="Room">
<button ng-click="updateMsg({name: name,room: room})">Start Talking!</button>


Answer Source

Every button inside form tag has default behavior - submit, to prevent it add type="button" to every button inside the form tag.

<button type="button" ng-click="NewConversation=true" >show login</button>