2017-07-04 52 views
1

我的網址是這樣的:AngularJS routeParam不工作

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

}); 

任何幫助將是一個生命的拯救。

+0

您的路線如何定義? – Sajeetharan

+0

我更新我的問題與航線的定義,請給它看看 – Mustapha

回答

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

+0

非常感謝你的回答和DEMO – Mustapha

1

如果宇已經路線deinition在控制器定義爲

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

}); 
+0

我試着你的建議,但仍然給我undefined! – Mustapha

+0

你能看到正確的視圖嗎?你也使用$ locationProvider.html5mode(因爲你的網址沒有#)? – Shantanu

+0

我不知道那個謝謝你的回答 – Mustapha