Problems using $state.go of the angular

Asked

Viewed 208 times

2

I have the following code:

.controller('cadastroCtrl', ['$scope', '$stateParams', '$http', function ($scope, $stateParams, $http, $cordovaSQLite, $window, $state) {

$scope.emailCli = [];
$scope.emailPro = [];

$scope.cadastrar = function(usuario){

    $http.post("http://ec2-54-68-29-61.us-west-2.compute.amazonaws.com/auth/register", usuario).success(function(response){

        var name = response.user.name;
        var email = response.user.email;
        var id = response.user.id;
        var is_professional = response.user.is_professional;    

        if(typeof(Storage) !== "undefined"){    

            if(is_professional == true){
                localStorage.setItem('userPro', name);
                $scope.emailPro = localStorage.setItem('emailPro', email);
                localStorage.setItem('idPro', id);

                console.log('Professional');

                $state.go('menuProfissional');
            }else{
                localStorage.setItem('userCli', name);
                $scope.emailCli = localStorage.setItem('emailCli', email);
                localStorage.setItem('idCli', id);

                console.log('Client');

                $state.go('menuCliente');
            }
        }else{
            console.log("Desculpe, mas o navegador nao possui suporte a Web Storage.");
        }

    })
}

}])

And on route.js

.state('menuCliente', {
    url: '/menuCliente',
    templateUrl: 'templates/menuCliente.html',
    controller: 'menuClienteCtrl'
  })

.state('menuProfissional', { url: '/menuProfissional', templateUrl: 'templates/menuProfissional.html', controller: 'menuProfissionalCtrl' })

And the following error warning appears on the console:

Cannot read Property 'go' of Undefined

What am I doing wrong?

1 answer

6


Your control initialization is incomplete, and because of this the reference to $state is with the value NULL.

You are using the declaration format of providers in array, however only 3 of the 6 of the function interface is being declared.

Where it reads:

.controller('cadastroCtrl', 
['$scope', '$stateParams', '$http', 
function ($scope, $stateParams, $http, $cordovaSQLite, $window, $state) {

Modify to:

.controller('cadastroCtrl', 
['$scope', '$stateParams', '$http', '$cordovaSQLite', '$window', '$state',
function ($scope, $stateParams, $http, $cordovaSQLite, $window, $state) {
  • 1

    Thanks man, that’s right.

  • 1

    +1 The answer should be accepted.

  • 1

    @Gustavosevero I’m glad it worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.