javascript - Angular 路线参数未定义

标签 javascript angularjs

我试图在我的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? 2-如果你想在你的配置中获取你的routeParams,那么你不能,

---您必须使用:

$route.current.params 改为

    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
            }
        }
    })
    });

但是在你的 Controller 中,你可以使用 $route.params 获取你的RouteParam,只要你在路由配置中定义了路由参数,如下所示:

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

注意如果你想定义任何路由参数,你必须使用(:),而不是(?)

关于javascript - Angular 路线参数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24190737/

相关文章:

javascript - 使用 javascript 删除上传的文件

javascript - 在 2 个类之间切换 Jquery

javascript - AngularJS 1.3 - 为什么表单在加载时有效,对于最小长度的输入?

javascript - 如何从 json 中检索 angularjs 表达式的数据?

javascript - 仅使用 javascript 和元素单击 angularjs 按钮

javascript - 将java脚本值设置为php session 并将 session 值获取到另一个php文件

javascript - Highcharts 上下文按钮颜色

javascript - 从 JavaScript 传递参数

javascript - Angular 和 Asp.Net,无法从脚本目录引用 Angular js

javascript - 使用 angularjs 从本地计算机调用服务器中的 python 函数