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?

Blackhole
  • 20,129
  • 7
  • 70
  • 68
Paul Kononenko
  • 9,407
  • 4
  • 18
  • 13

1 Answers1

1

I'd propose something else. let the $routeProvider "normal" and use the controller to set the path to a ng-include in your template :

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

in the PageController , inject $routeParams , and use it to modify the url of an include in your template.

$scope.include_url = $routeParams.page ; 

finaly in the page.html template , wire things up :

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

it has not been tested , i'm open to modifications and suggestions.

mpm
  • 20,148
  • 7
  • 50
  • 55
  • 1
    Are you basically suggesting the answer in this post? http://stackoverflow.com/questions/11534710/angularjs-how-to-use-routeparams-in-generating-the-templateurl – dnc253 Jan 11 '13 at 21:17
  • i was not aware of such an answer but i guess it is that solution basically. – mpm Jan 11 '13 at 21:44