Question mark added in the middle of url on form submit

When I click the submit button on the form it adds? right before # so / app / # / pageName changes to / 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>

      

+3


source to share


1 answer


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

in my ng-submit I had to add the $ event as parameter that passed the event to my submit function and then I was able to do event.preventDefault () to prevent the route from being changed. Not sure if this is a bug in angular or if it is intended to be the behavior, but hopefully this helps someone else.



So here's 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>

      

+6


source







All Articles