0

I find unsatisfactory solve, where templateUrl define at routeProvider, and at controller it redefine. But it is one excess request. (PS: I have my templates at any folder and I have hash-routes with name of this templates, how can I do this(render my template at current hash) correctly?)

For example this code render my index.html template at ng-view tag:

angular.module('amiu', [])
.config(function($routeProvider){
  $routeProvider
  .when('/index/', {templateUrl:'partitials/index.html'})
})

But I want to do that:

angular.module('amiu', [])
.config(function($routeProvider, $routeParams){
  $routeProvider
  .when('/:page/', {templateUrl:'template/'+$routeParams.page+'.html'})
  .otherwise({redirectTo:"/"})
})

How can I do that?

4

1 回答 1

1

我会提出别的建议。让 $routeProvider “正常”并使用控制器在模板中设置 ng-include 的路径:

 $routeProvider
  .when('/page/:page', {templateUrl:'template/page.html',
     controller:"PageController"});

在 PageController 中,注入 $routeParams ,并使用它来修改模板中包含的 url。

$scope.include_url = $routeParams.page ; 

最后在 page.html 模板中,连接起来:

<div data-ng-include="'template/page-'+include_url+'.html"></div>

它尚未经过测试,我愿意接受修改和建议。

于 2013-01-11T20:35:42.057 回答