Skip to content

Commit dc9be17

Browse files
author
Jamie Pate
committed
Altered routing to allow interpolation of params into templateUrl
added test for same
1 parent d6706ef commit dc9be17

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/ng/route.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,15 @@ function $RouteProvider(){
420420
if (!match && (params = matcher($location.path(), path))) {
421421
match = inherit(route, {
422422
params: extend({}, $location.search(), params),
423-
pathParams: params});
423+
pathParams: params
424+
});
425+
if ((route.templateUrl) && (/:/.test(route.templateUrl))) {
426+
match.templateUrl = interpolate(route.templateUrl,params);
427+
}
428+
//remove cached template if params were substituted (rely on browser cache)
429+
if (match.templateUrl != route.templateUrl) {
430+
match.template = undefined;
431+
}
424432
match.$route = route;
425433
}
426434
});

test/ng/routeSpec.js

+27
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,33 @@ describe('$route', function() {
318318
});
319319
});
320320

321+
it('should interpolate parameters into the url of fetched templates',function(){
322+
module(function($routeProvider) {
323+
$routeProvider.
324+
when('/foo/:rparam',{templateUrl:':rparam.html'});
325+
});
326+
327+
inject(function($route, $httpBackend, $location, $rootScope) {
328+
var log = '';
329+
$rootScope.$on('$routeChangeStart', function(e, next) { log += '$before(' + next.templateUrl + ');'});
330+
$rootScope.$on('$routeChangeSuccess', function(e, next) { log += '$after(' + next.templateUrl + ');'});
331+
332+
$httpBackend.expectGET('r1.html').respond('R1');
333+
$httpBackend.expectGET('r2.html').respond('R2');
334+
335+
$location.path('/foo/r1');
336+
$rootScope.$digest();
337+
expect(log).toBe('$before(r1.html);');
338+
339+
$location.path('/foo/r2');
340+
$rootScope.$digest();
341+
expect(log).toBe('$before(r1.html);$before(r2.html);');
342+
343+
$httpBackend.flush();
344+
expect(log).toBe('$before(r1.html);$before(r2.html);$after(r2.html);');
345+
expect(log).not.toContain('$after(r1.html);');
346+
});
347+
});
321348

322349
it('should not update $routeParams until $routeChangeSuccess', function() {
323350
module(function($routeProvider) {

0 commit comments

Comments
 (0)