0

I am using AngularJS ui-router to go to different pages. I have a button that submits a form but I also want it to take me to a different page upon submission. So I tried to do something like this:

Approach 1, button

<button href="#anotherPage" type = "submit" ng-click = "someFunction()" class = "btn btn-default">Go to another page</button> 

Typically, this is done in a link, so I also tried this:

Approach 2, link

<a href="#anotherPage" type = "submit" ng-click = "someFunction()" href="#results" class = "btn btn-default">Go to another page</a> 

Approach 1 doesn't work because href doesn't work with buttons, approach 2 takes me to the other page but does not submit. What is the cleanest way to do this?

kevin_b
  • 803
  • 4
  • 16
  • 34
  • maybe this help http://stackoverflow.com/questions/33460529/angularjs-and-reloading-and-redirect-after-submit-using-ui-router/33461052 – AVI Apr 07 '16 at 17:47

1 Answers1

3

You can use $state.go() in your click method defined in your controller.

like -

Button Code -

<button ng-click="someFunction()" class="btn btn-default">Go to another page</button>

Method in controller -

$scope.someFunction = function() {
 // Process the data 
 $state.go('newsstate',{id: id});
};

Do not forget to inject $state.

As a suggestion- whenever you are using ui-router try to us ui-sref instead of simple hrefs.

swapnesh
  • 26,318
  • 22
  • 94
  • 126