Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$state, $stateParams, getting undefined object

I am getting unexpected results from both methods.

I have my $state configed

    $stateProvider
                .state('status', {
                  url: "/status/:payment",
                  controller: 'QuestCtrl',
                  templateUrl: "index.html"
                });

And on the Controller I have:

    angular.module('quest').controller('QuestCtrl',function($scope,$stateParams,$state){

     console.log($stateParams.payment); // undefined

     console.log($state); // Object {params: Object, current: Object, $current: extend, transition: null}

}

I already used $stateParams in other projects and it worked but now I can't figure out what is going on here..

like image 907
Gabriel Lopes Avatar asked Jun 20 '15 09:06

Gabriel Lopes


2 Answers

        ['$scope','$stateParams','$state',
function($scope,  $http,          $stateParams, $state)

The names of the services don't match with the variables.

So $http is actually the $stateParams service, $stateParams is actually the $state service, and $state is undefined.

My advice: stop using this array notation, which clutters the code and is a frequent source of bugs. Instead, use ng-annotate as part of the build procedure, which will do it, correctly, for you.

like image 121
JB Nizet Avatar answered Oct 10 '22 18:10

JB Nizet


As I already commented above You forgot to inject $http service

angular.module('quest').controller('QuestCtrl',
['$scope','$http','$stateParams','$state',function($scope,$http,$stateParams,$state){

 console.log($stateParams); // Empty Object
 console.log($stateParams.payment); // Empty Object

 console.log($state); // I get the entire state, I can see that my params are there.

console.log($state.params);
}

So your parameters mismatch and it turns out you will get $state in $stateparms and $state is empty. And $http hold $state :P

Hope it helps :)

like image 22
squiroid Avatar answered Oct 10 '22 17:10

squiroid