Get route parameters from URL

I am trying to extract a reset pw token from a link in a reset pw email. The link sends the user back to my node / angular application with the token I am trying to get.

Laravel API Template:

<td class="content"> <a href="http://localhost:3000/reset-password?token={{$token}}">Reset Your Password</a> </td> 

Node / angular app: ResetPassword.ejs template: I use an angular controller:

 <div ng-controller="resetPasswordCtrl"> ...stuff </div> 

reset Password Controller:

 'use strict'; angular .module('myApp') .controller('resetPasswordCtrl', ['$scope', '$routeParams', '$location', function($scope, $routeParams, $location) { console.log('ROUTE PARAMS', $routeParams); //object with a bunch of getters/setters console.log('ROUTE PARAMS', $routeParams.token); //undefined console.log('Location', $location.search('token')); //LocationHashbangUrl console.log('Location', $location.search().token); //true console.log('Location', $location.search()['token']); //true console.log('Route current params', $route); //empty route object }]); 

For LocationHashbangUrl ( $location.search('token') ), I know that I get the correct URL with a token because it shows $$ absUrl.

enter image description here

Why can't I get the token parameter using one of these methods shown on the controller?

+4
source share
2 answers

Turns off without using html5 / angular routing, typical methods

 $location.search() $routeParams 

will not work.

Since I pass parameters and access my node application from the outside (from an email link distributed from laravel), I needed to parse the URI using javascript.

I found this resource that simplifies it. So, the following works:

 'use strict'; angular .module('myApp') .controller('resetPasswordCtrl', ['$scope', '$window', function($scope, $route, $window) { var getURIParams = function(variable) { var query = $window.location.search.substring(1); var vars = query.split('&'); for (var i = 0; i < vars.length; i++) { var pair = vars[i].split('='); if (decodeURIComponent(pair[0]) == variable) { return decodeURIComponent(pair[1]); } } console.log('Query variable %s not found', variable); }; console.log('VAR', getURIParams('token')); //my token param }]); 
0
source

Can you post your $ routeProvider? You could just add the token as a parameter, and then $ routeParams.token will do what you want. Something like that:

 $routeProvider.when('/reset-password/:token') 

That would mean that your reset url would look like this:

 http://localhost:3000/reset-password/{{$token}} 
0
source

All Articles