Matoy Matoy - 4 months ago 23
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

ng-if
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?

thanks

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

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

</form>

Answer

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>