Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

question mark added in middle of url on form submit

Tags:

angularjs

When I click the submit button in a form it is adding a ? right before the # so /app/#/pageName changes into /app/?#/pageName. Is this normal behavior? Code is just basic stuff.

angular.module('myApp', [])
.controller('MyCtrl', function ($scope) {
    $scope.submit = function() {

    };
});

<form ng-controller="MyCtrl" ng-submit="submit()">
    <button>Submit</button>
</form>
like image 336
Brian Avatar asked Mar 18 '23 10:03

Brian


1 Answers

I finally found the answer thanks to Angular JS does not allow preventDefault or return false to work on form submission.

in my ng-submit I had to add $event as parameter which passed the event to my submit function and I was then able to do event.preventDefault() to prevent the route from changing. Not sure if this is a bug in angular or if it's intended behaviour but hopefully this will help someone else.

So here is the fixed code:

angular.module('myApp', [])
.controller('MyCtrl', function ($scope) {
    $scope.submit = function(event) {
        event.preventDefault();
    };
});

<form ng-controller="MyCtrl" ng-submit="submit($event)">
    <button>Submit</button>
</form>
like image 197
Brian Avatar answered Apr 08 '23 08:04

Brian