Prevent page reloading after I submit form in Angular

4,166 views
Skip to first unread message

Funny Cherry

unread,
Apr 22, 2015, 1:19:37 AM4/22/15
to ang...@googlegroups.com
Hi.
I have a form kind of:

          <div class="input">
            <div id="chat" data-channel="/messages" data-recipient-id="3"></div>
            <form class="new_message ng-pristine ng-valid" ng-action="http://localhost:3000/api/v1/messages" name="NewMessageForm" ng-submit="submit()" novalidate ng-controller="MessagesController as MsgCtrl" id="new_message" accept-charset="UTF-8" data-remote="true"><input name="utf8" type="hidden" value="✓">
              <textarea name="new_message_text" id="message_text" ng-model="NewMessageFormData.text"></textarea>
              <input type="hidden" ng-init="NewMessageFormData.recepientId = 3" ng-model="NewMessageFormData.recepientId">
            </form>
          </div>

After I submit this form and after submit() function finish work, I get page reload.
How to prevent it?

Caitlin Potter

unread,
Apr 22, 2015, 1:27:41 AM4/22/15
to ang...@googlegroups.com
If the 'action' attribute is there, my-submit still not prevent default. Without the action attribute, the user agent won't send your data. Consider sending your form via XHR and FormData instead, in order to send the form exactly as the browser would normally, without triggering navigation.
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Funny Cherry

unread,
Apr 22, 2015, 3:58:26 AM4/22/15
to ang...@googlegroups.com
Solved: involved jquery-ujv via Bower, added data-remote=true attribute to the <form> tag

среда, 22 апреля 2015 г., 12:19:37 UTC+7 пользователь Funny Cherry написал:

Funny Cherry

unread,
Apr 22, 2015, 4:22:31 AM4/22/15
to ang...@googlegroups.com
But in this case ng-submit="submit()" stopped working (just no call for this function).
wtf


среда, 22 апреля 2015 г., 12:19:37 UTC+7 пользователь Funny Cherry написал:
Hi.
Reply all
Reply to author
Forward
0 new messages