2016-06-07 85 views
0

我工作的一個應用程序有關路由,我的代碼:Angularjs模板無法工作

//HTML, I passed a 'test' into routing 
<a href="#details/test">Test</a> 
<div data-ng-view=""></div> 

//Template 
<h1>{{res}}</h1> 

//Angularjs 
var app = angular.module("dictApp", ['ngRoute']); 

app.config(['$routeProvider', function ($routeProvider) { 
    $routeProvider. 
    when('/details/:test', { 
     templateUrl: 'template.html', 
     controller: 'testCtrl' 
    }); 
}]); 

app.controller('testCtrl', function ($scope, $routeProvider) { 
    $scope.res = $routeProvider.test; 
}); 

//The template is displayed as 
{{res}} 

模板頁面應顯示「測試」,但我不知道爲什麼它沒有工作。

Thansk提前。

回答

1

'test'參數應該在$ routeParams中可用。

app.controller('testCtrl', function ($scope, $routeParams) { 
    $scope.res = $routeParams.test; 
}); 
1

暴露路由參數的服務是$routeParams$routeProvider是用於在使用類似的方法.when以及

你需要注入$routeParams,並用它來代替$routeProvider

app.controller('testCtrl', function ($scope, $routeParams) { 
    $scope.res = $routeParams.test; 
}); 
你已經在你的代碼做了一個應用程序配置路由提供商