3

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 {}
  }
]);
1
  • 1
    You could use $rootScope Commented Aug 21, 2015 at 14:35

2 Answers 2

4

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
           });

     }
]);
Sign up to request clarification or add additional context in comments.

Comments

0

Make sure you inject $stateParams into your controller? Can you provide a code snippet of what you already have?

1 Comment

Injecting $stateParams in a controller outside ui-view gives me an empty object ({}).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.