简体   繁体   中英

AngularJS Submit Form On Submit

I'm trying to submit a form with AngularJS without use $http. I'm checking the form via Javascript, but not sure how to submit after the check is complete. Sample code looks like this:

AngularJS

 app.directive('checkLoggedinSubmit', ['account_id', 'session', 'LoginModal',
    function(account_id, session, LoginModal) {
        return {
            restrict : 'A',
            link : function(scope, element, attrs) {

                element.bind('submit', function(e) {
                    e.preventDefault();             

                    //If not logged in, show logged in, otherwise subit
                    if (!account_id && !session.read('account_id')) {
                        LoginModal.show();
                    } else {
                        scope.$eval(attrs.checkLoggedinSubmit);
                        scope.$apply();
                        //Tried To use JQuery
                        //$(e.currentTarget).submit();
                    }

                });

            }
        };

}]);

HTML Form

<form role="form" enctype="multipart/form-data" method="post" action="/file/submit" class="well" style="padding: 0px 20px 20px;" check-loggedin-submit >
    <div class="form-group">
        <input type="file" name="my_file" />
    </div>

    <div class="form-group">
        <input type="submit" class="btn btn-success"  />
    </div>

</form>

I tried to have JQuery submit the form, but threw errors. Anyone now how to solve this?

better way to submit form in angular is using "ng-sbmit"

you can use something like ng-submit="functionHandlingSubmitTask()"

you can look into given link to

http://learnwebtutorials.com/angularjs-tutorial-submitting-form-ng-submit

try it:

<input type="submit" data-ng-click="save(input) class="btn btn-success"  />

save is the JQuery method name and input is params.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM