angularjsangular-ui-routerstateparam

angularjs back previous page state.go stateParams


Hello all I want to know how to make a return to the previous page in AngularJS with state.go stateParams or between two different pages I tried this but the variable is empty:

   $ctrl.next = function(){

        $state.go('modiffili', {object:'test'});

    };

and this controller of my second page :

(function (angular) {
'use strict';

modiffiliCtrl.$inject=['$state','$stateParams'];


function modiffiliCtrl($state,$stateParams) {
    var $ctrl = this;


    console.log('modiffiliController');
    console.log($state.params.object);
   $ctrl.retour = function () {
        $state.go('^');
}

I can't to use rootScope. thank you to help me !


Solution

  • You can listen to $stateChangeStart event, and save the name and params of previous state in order to be able to return to it.

    $rootScope.$on('$stateChangeStart', function (event, toState, toStateParams, fromState, fromParams) {
       $rootScope.previousState = fromState;
       $rootScope.previousStateParams = fromParams;
    });
    

    And then you can call $state.go($rootScope.previousState, $rootScope.previousStateParams) if you need it