How to pass parameters and catch them with angular $state.go?

Asked

Viewed 726 times

1

I have the following code in the controller

app.controller('loginCtrl', ['$scope', '$stateParams', '$http', '$cordovaSQLite', '$window', '$state',

function ($scope, $stateParams, $http, $cordovaSQLite, $window, $state) {

$scope.clienteId = null;
$scope.clienteNome = null;
$scope.clienteEmail = null;

$scope.profId = null;
$scope.profNome = null;
$scope.profEmail = null;

$scope.logar = function(usuario){

    $scope.error = null;
    $scope.errors = {};

    $http.post("http://vigilantescomunitarios.com/serviapp/api_gustavo/login.php", usuario).success(function(response){
        //console.log(response);
        if(response == '' || response == [] || response == 'null'){
            $scope.msgErro = "E-mail ou senha inválido";
            return;

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

            var prof = response.e_profissional;
            var id = response.idusuario;
            var nome = response.nome;
            var email = response.email;

            if(prof == 1){
                $scope.profNome = nome;
                $scope.profEmail = email;
                $scope.profId = id;
                console.log($scope.profNome);

                $state.go('menuProfissional', { "nome": nome, "email": email, "id": id });
            }else{
                $scope.clienteId = id;
                $scope.clienteNome = nome;
                $scope.clienteEmail = email;
                console.log($scope.clienteNome);

                $state.go('menuCliente');
            }
        }

        })

     }

}])

And the controller where I’ll get them

app.controller('menuProfissionalCtrl', ['$scope', '$stateParams', 'stateParams', function ($scope, $stateParams, stateParams) {

$state.params.nome;
console.log($state.params.nome);


}])

However, what appears on the console is "Nan"

What I have to fix to get the name, email and id?

  • Where your route is set menuProfissional?

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

  • To get the value you just need to use $stateParams.nome inside your controller. Test to see if it fits

1 answer

1


To get the value you must use the service $stateParams which will contain the value that was passed by $state.go. For example, in the menuProfissionalCtrl:

...
console.log($stateParams.nome);
...

Tip: This link contains a best practice manual that facilitates maintenance and code creation using Angular 1.x.

Browser other questions tagged

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