Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular UI Router: Access state parameters outside ui-view

The menu is located outside a <div> block which is rendered via UI Router's ui-view.

In order to modify the menu according to the state parameters, I need access to the current state parameters from within the menu's controller, but the $stateParams variable is an empty object when used outside the ui-view part.

How can I access them?

angular.module('myapp').controller('MenuCtrl', ['$scope', '$rootScope', '$stateParams', '$meteor', '$filter',
  function($scope, $rootScope, $stateParams, $meteor, $filter) {
    // ... $stateParams equals {}
  }
]);
like image 889
ideaboxer Avatar asked Jun 05 '26 15:06

ideaboxer


1 Answers

Try to watch for state changes in your controller...

angular.module('myapp').controller('MenuCtrl', ['$scope', '$rootScope', '$meteor', '$filter',
    function($scope, $rootScope, $meteor, $filter) {

        //this watches for state changes
        $rootScope.$on('$stateChangeStart', 
           function(event, toState, toParams, fromState, fromParams){
              //do something when state changes
              state = toState.name;
              postid = toParams.postid;
              console.log(toParams); //this is the stateParams you need
           });

     }
]);
like image 50
maztch Avatar answered Jun 07 '26 09:06

maztch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!