0

I'm trying to get a property on the $stateParams object within a service and when I console.log($stateParams), it shows the property 'playerId' holding a value but when I console.log($stateParams.playerId), it shows undefined:

    app.factory('playerService', ['$http', '$state','$stateParams', 
        function ($http, $state, $stateParams) {

            var playerId = $stateParams.playerId;

            console.log($stateParams);  // logs - object{ playerId:"555"} 
            console.log($stateParams.playerId); // logs - undefined
        }

Here's my state:

    $stateProvider
        .state('headless' , {
          abstract: true,
            url: "/:playerId",
            resolve: {
              liveData: function (playerService, $stateParams) {
                var live = playerService.getLive($stateParams.playerId); 
                return live;
              }
            },
            data: {
              headless: true
            }
       }.state('player.live', {
            url: "^/live/:playerId",
            templateUrl: "views/main_live_video_chat.html"
       })

Any ideas what's happening?

Ben Roberts
  • 63
  • 2
  • 12
  • maybe you have a typo, is this code same as you have in your project, did you copy-paste it? – mbeso Mar 06 '17 at 22:56
  • 3
    I think that you are trying to access the `$stateParams` before the state is ready... The question is, why your are using `$stateParams` in a factory? If you can describe a bit more your problem or what a re you trying to do, I could help you to find an alternative way to do it... Anyway, check this post [ui-router-use-stateparams-in-service](http://stackoverflow.com/questions/21452537/ui-router-use-stateparams-in-service) – The.Bear Mar 06 '17 at 22:56
  • You're right, Bear. When I moved the declaration into a function within the service, it worked fine, Thanks! – Ben Roberts Mar 07 '17 at 00:42

0 Answers0