AngularJS routeParam无效

时间:2017-07-04 16:32:07

标签: angularjs angularjs-scope mean-stack

我的网址是这样的:

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

我希望得到参数id

59567bc1de7ddb2d5feff262

但由于某些原因,routeParam始终返回undefined

我的控制器是:

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

});

任何帮助都将成为救命。

2 个答案:

答案 0 :(得分:1)

如果yu的路由定义定义为

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

});

答案 1 :(得分:1)

确保您已定义了如下所述的路线网址

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