2

this bites me too, like many others I have a simple ng-form (: cleared for bravity) in a partial:

<form ng-submit="functionName()">


                   <input type="text" class="postField" ng-model="model.text" required ng-maxlength=200 />
            <button class="postBT" ng-click="functionName()" ng-class="BToverclass" ng-mouseover="BToverclass = 'overShadow'" ng-mouseleave="BToverclass=''">Post</button>
            </div>

            </form>

for some reason every form submit, we get 2 posts to the controller, with all the data doubled. I checked and the specific controller doesn't appear in the html, but only in the route. Any idea what I'm missing?

Thanks!

Community
  • 1
  • 1
alonisser
  • 11,542
  • 21
  • 85
  • 139

1 Answers1

3

You have both an ng-click() calling functionName() and a call to it from ng-submit. Each results in a call to your function. You only want the submit one.

FYI, you also have a </div> with no opening <div> for it to close.

Here's working code:

  <form ng-submit="functionName()">
      <input type="text" class="postField" ng-model="model.text" required ng-maxlength=200 />
      <button class="postBT" ng-class="BToverclass" ng-mouseover="BToverclass = 'overShadow'" ng-mouseleave="BToverclass=''">Post</button>

   </form>
KayakDave
  • 24,636
  • 3
  • 65
  • 68
  • Thanks! I'll check it tomorrow. and if the form has another button to submit in another option(to the same controller)?, should I remove ```ng-submit```or would ng-submit (since it's the same function) take care of both input fields and buttons (one for uploading files..)? – alonisser Oct 21 '13 at 21:15
  • The ng-submit will handle all the form elements in the same way submit does in traditional html. You probably want to keep the ng-submit. For the file upload you might want to check out ngUpload: http://twilson63.github.io/ngUpload/ – KayakDave Oct 21 '13 at 22:56