Which is fired first: submit or ng-click?

First of all, excuse me if my english is bad, it is not my native language.

So, I have a search form that returns a maximum of 20 items, and if there are more, then the user can switch between pages. Thus, there will be an unknown number of pages. This is how I do it right now: (example with two pages)

    <form class="searchForm" method="POST" action="~/Search/Products">
                            <input type="hidden" name="keyword" value="@Model.Keyword" />
                            <input type="hidden" name="pagenum" value="{{choosedPage}}" id="page" />

                            <button type="submit" ng-click="choosedPage=1" class="btn btn-primary">1</button>
                            <button type="submit" ng-click="choosedPage=2" class="btn btn-primary">2</button>
                    </form>

      

So when the user clicks on the button, he changes the "choosedPage" and directly triggers the submit action with the page we want. I check what works fine .

But it seems pretty hacky, but what if the submit action is fired before the ng-click action? Is it possible? Do you have another idea what this is (and besides creating button 1 on the page). Thank.

+3


source to share


1 answer


So, in the first place, you don't need Angular to do what you do. You can simply assign name

and value

buttons with type="submit"

.

<form class="searchForm" method="POST" action="~/Search/Products">
   <input type="hidden" name="keyword" value="@Model.Keyword" />

   <button type="submit" name="pagenum" value="1" class="btn btn-primary">1</button>
   <button type="submit" name="pagenum" value="2" class="btn btn-primary">2</button>
</form>

      

The web really existed before Angular, you know :)

But if you need to do some additional Angular tasks, you can use ng-submit

on the form element instead of ng-click

on the button:



<form ng-submit="doSomethingBeforeSubmit($event)" 
      method="POST" action="~/Search/Products">
   ...
</form>

      

This will be $scope.doSomethingBeforeSubmit

called before the form is submitted. $event

is a special variable that you can pass to your doSomethingBeforeSubmit

function.

Here's the ng-submit

documentation.

+1


source







All Articles