Clicking the button will cause the form to refresh.
P粉523625080
P粉523625080 2023-08-14 20:46:02
0
2
419

I have a form in Angular with two button labels. A button submits the form on ng-click. The other button is purely for navigation using ng-click. However, when this second button is clicked, AngularJS causes the page to refresh, triggering a 404 error. I set a breakpoint in the function and it triggers my function. If I do any of the following, it stops:

  1. If I remove ng-click, the button does not cause the page to refresh.
  2. If I comment out the code in the function, it does not cause the page to refresh.
  3. If I change the button label to an anchor label () and add href="" it does not cause a refresh .

The latter seems to be the easiest solution, but why does AngularJS run any code after my function that causes the page to reload? Looks like a bug.

This is the form:

This is the controller method:

$scope.showChangePassword = function() { $scope.selectedLink = "changePassword"; };


P粉523625080
P粉523625080

reply all (2)
P粉637866931

You can try blocking the default handler:

html:

js:

$scope.saveUser = function (event) { event.preventDefault(); // 你的代码 }
    P粉821274260

    If you look at theW3C spec, it seems like the obvious thing to try is to marktype='button'on the button element that you don't want submitted.

    Special attention should be paid to the location it says

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!