AngularJS - How do I change the State from Inside the Controller

Submit of the catch, my page will change to the route page. This works fine, however imagine a scenario where I need to do some rationale in my controller to start with, and after that based off the outcomes, I would need to change to a particular page. How would I do this from inside the controller, versus from a catch click.

There are many way to change the state in angular js

<button ng-click="changeState()">Change State</button>

First way to change the state in angular

var myFirstApp = angular.module('myFirstApp', []);
  myFirstApp.controller('myCtrl', function($scope) {
   
   $scope.changeState = function(){
     $state.go('app/login');
   };
  });

Second way to change the state in angular

var myFirstApp = angular.module('myFirstApp', []);
  myFirstApp.controller('myCtrl', function($scope ,  $location) {
   
   $scope.changeState = function(){
     $location.path('app/page-name');
   };
  });

Third and simple way to change the state in angular

var myFirstApp = angular.module('myFirstApp', []);
  myFirstApp.controller('myCtrl', function($scope ,  $location) {
   
   $scope.changeState = function(){
     window.location.href = "#/app/accounts";
   };
  });

Share this

Related Posts

Previous
Next Post »