且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AngularJS:从控制器内读取路由参数

更新时间:2023-12-02 08:41:22

问题是你可能在之前运行的控制器中注入了 $routeParams$route发生了路线变化,例如您的主/主/页面控制器.

The problem is that you probably inject $routeParams or $route in a controller that is run before a route change has occurred, e.g. your main/master/page controller.

如果您将 $routeParams 注入控制器中的特定路由(在定义路由时由 controller 属性指定),那么它将起作用,否则您***听一下路由服务广播的各种事件.

If you inject $routeParams in a controller for a specific route (specified by the controller property when you define the route), then it will work, otherwise you're probably better of listening to the various events the route service broadcasts.

尝试更改您的代码以使用

Try to change your code to use

$scope.$on('$routeChangeSuccess', function (ev, current, prev) {
   // ... 
});