question mark added in middle of url on form submit

后端 未结 1 772
不知归路
不知归路 2021-01-06 11:58

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 st

相关标签:
1条回答
  • 2021-01-06 12:36

    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>
    
    0 讨论(0)
提交回复
热议问题