Angular的routeParams没有定义

Angular routeParams is undefined

本文关键字:定义 routeParams Angular      更新时间:2023-09-26

我试图在我的JS中使用routeParams,但它总是传递为未定义。

app.controller('ShowKey', function($scope, $http, $routeParams, $log) {
  $log.info('SomeCtrl - starting up, yeah!');
  $log.info($routeParams);
  $scope.getKey = function() {
    $log.info($routeParams);
  };
});

除了用routeParams调用app.controller之外,还有什么需要设置的吗?

1-你在配置中考虑过任何routeParams吗?如果你想在你的配置中得到你的routeParams,那么,你不能,

——你必须使用:

route.current美元。参数,而不是

    app.config(function($routeProvider){
      $routeProvider.when('/path/:yourRouteParam'
       templateUrl:"your template url address",
        controller:'your controller',
        resolve: {
            resolbeObject: function($route){
             //**here you cannot use $routeParam to get "yourRouteParam"** property
            // you must use : $route.current.params
            }
        }
    })
    });

但是在你的控制器中你可以用$route获取你的routeparam。参数,只要你在路由配置中像这样定义了路由参数:

       $routeProvider.when('/path/:**yourRouteParam**

说明如果要定义任何路由参数,必须使用(:),而不是(?)