Vinicius Sousa
Vinicius Sousa

Reputation: 75

Param in $routeParams is undefined but $routeParams not

In code above, the first console.log returns undefined

But the second return a object with cpf property is set. Why ?

My URL: http://xxxxxxx/#!/cpf/68473303253

My Module

angular.module('welcomeback').controller('welcomeback', ['$scope', '$location', '$routeParams', function($scope, $location, $routeParams){

console.log('First console log');
console.log($routeParams.cpf);
console.log('Second console log');  
console.log($routeParams);

$routeParams

Plunker: https://plnkr.co/edit/Exbherhe6TNYb9t5ThfL

Upvotes: 1

Views: 925

Answers (1)

Sumama Waheed
Sumama Waheed

Reputation: 3609

Because the route change is still under process when the controller loads and the first console is printed.

From Angular:

Note that the $routeParams are only updated after a route change completes successfully. This means that you cannot rely on $routeParams being correct in route resolve functions. Instead you can use $route.current.params to access the new route's parameters.

Upvotes: 2

Related Questions