AngularJS 路由参数不工作

AngularJS routeParam not working

我有 url 这样的:

http://localhost:3000/details/59567bc1de7ddb2d5feff262

我想获取参数id

59567bc1de7ddb2d5feff262

但是由于某些原因routeParam总是returnsundefined

我的控制器是:

task.controller('ctrla',  function($rootScope, $scope, $http, $timeout, $routeParams){
    $scope.first = 1;
    console.log($routeParams);        
});

路线:

task.config(function ($routeProvider, $locationProvider){
$routeProvider.when('/details/:id', {
      templateUrl: "details.html",
      controller: "ctrla"
    })

});

任何帮助都会挽救生命。

如果您将路由定义定义为

$routeProvider.when('/details/:id', {
      templateUrl: "partial.html",
      controller: "ctrla"
    })

然后在控制器中你应该得到它的值

task.controller('ctrla',  function($rootScope, $scope, $http, $timeout, $routeParams){
    $scope.first = 1;
    console.log($routeParams.id);

});

确保您已经定义了如下所述的路由 url,

app.config(['$routeProvider', function($routeProvider) {
  $routeProvider
    .when('/home', {
      template: "HI this is home Screen",
      controller: 'ctrla'
    })
    .when('/details/:id', {
      templateUrl: "template.html",
      controller: 'profileController'
    })
    .otherwise({
      redirectTo: '/home'
    })
}]);

DEMO