Angularjs is clearing the Url parameters

Asked

Viewed 168 times

1

I have a system with Angular and Java.

On the initial route I read the url parameters (some user information such as code, etc.) and saved in $rootScope to access throughout the application.

If I access directly through the browser, works accordingly, the example of the link accessed is this:

http://localhost:3000/#! /login? parametro1=10&parametro2=20

But I have another system that generates these links and sends them by e-mail to some recipients, and they access from the email, with these parameters embedded in the url.

In such cases, Angular loads the main link, but it cleans all the parameters, leaving the url only:

http://localhost:3000/#! /login

To read the parameters, I am using $Location.search() with Angular.

Edit:

I recover the parameters in the "module.run()" of the application’s Angular.

    function run($rootScope, $location) {

     var data = $location.search();

     if (data) {
         $rootScope.parametro1= data.parametro1;
         $rootScope.parametro2= data.parametro2;
     } 

   }

Angular routes

function config($stateProvider, $urlRouterProvider) {

    //Route Default
    $urlRouterProvider.otherwise('/login');

    //Routes
    $stateProvider
        .state('login', {
            url: '/login',
            templateUrl: 'templates/login.html',
            controller: 'AuthenticationCtrl',
            controllerAs: 'vm'
        })

}

Login Controller

function AuthenticationCtrl($state, $rootScope, $location) {

   var vm = this;
   vm.init = init;

   init();

   function init() {

        //Faço o parse, pois esse parâmetro é sempre inteiro..
        vm.parametro1 = parseInt($rootScope.parametro1);
    }

}

Does anyone know why this behavior?

How to fix this?

  • Could show the code snippet where you prepare the routes, Geferson?

  • Of course @Onosendai, I already update the post.

1 answer

1


Standard route handling does not preserve querystrings, and your route is not waiting parameters. For all purposes, the route

#!/login?parametro1=10&parametro2=20

It is non-existent, so the Router URL forwards the user to the parameter set in .otherwise().

If you want to receive parameters on your route, set it as follows:

$stateProvider
    .state('login', {
        url: '/login/:parametro1/:parametro2',
        templateUrl: 'templates/login.html',
        controller: 'AuthenticationCtrl',
        controllerAs: 'vm'
    })

And send your Urls in the following format:

#!/login/[parametro1]/[parametro2]

In your example, it would look like this:

#!/login/10/20

Browser other questions tagged

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