javascript - $routeParams 中的参数未定义,但 $routeParams 未定义

标签 javascript angularjs

在上面的代码中,第一个 console.log 返回未定义

但第二个返回一个已设置 cpf 属性的对象。为什么?

我的网址:http://xxxxxxx/#!/cp​​f/68473303253

我的模块

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

骗子:https://plnkr.co/edit/Exbherhe6TNYb9t5ThfL

最佳答案

因为当 Controller 加载并打印第一个控制台时,路由更改仍在进行中。

来自 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.

关于javascript - $routeParams 中的参数未定义,但 $routeParams 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41896810/

相关文章:

javascript - 为什么 react 路由器中的链接标签没有显示我想要的页面?

javascript - 当方法不返回任何内容时,那么不是一个函数 - Javascript

javascript - 当 AJAX 更新我的页面上的数据时更新 HighCharts 图表

html - 如何使用angular js从数组制作html表格

javascript - AngularJS,将这两种方法合二为一

javascript - Extjs Ajax请求中未获取文件数据

javascript - 根据屏幕大小更改 $scope 变量

angularjs - 在我的应用程序初始加载时,为什么未设置 ui-router 状态?

javascript - 不同父页面的不同样式表

javascript - 当 div 具有特定类时更改元素 css