Angularjs模板无法正常工作

时间:2016-06-07 04:02:31

标签: angularjs angularjs-routing angularjs-templates

我正在开发一个关于路由的应用,我的代码:

//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}}

模板页面应该显示'test',但我不知道它为什么不起作用。

Thansk提前。

2 个答案:

答案 0 :(得分:1)

'test'参数应该在$ routeParams中可用。

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

答案 1 :(得分:1)

公开路由参数的服务是$routeParams$routeProvider是用于在应用中配置路由的提供程序,就像您使用.when方法在代码中完成的路由一样

您需要注入$routeParams并使用它而不是$routeProvider

app.controller('testCtrl', function ($scope, $routeParams) {
  $scope.res = $routeParams.test;
});
相关问题