Skip to content

Commit 8d1ac0d

Browse files
committed
template and templateUrl can be set as injectable functions
1 parent bb0b8da commit 8d1ac0d

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ Adds new segment at current pointer level.
179179
180180
Segment's parameters hash. The following params are supported:
181181
182-
- `template` provides HTML for the given segment view;
183-
- `templateUrl` is a template which should be fetched from the network via this URL; if neither `template` nor `templateUrl` parameters are defined, the DOM element's transcluded content will be used;
182+
- `template` provides HTML for the given segment view; if `template` is a function, it will be called with injectable arguments;
183+
- `templateUrl` is a template which should be fetched from the network via this URL; if `templateUrl` is a function, it will be called with injectable arguments; if neither `template` nor `templateUrl` parameters are defined, the DOM element's transcluded content will be used;
184184
- `controller` is attached to the given segment view when compiled and linked, this can be any controller definition AngularJS supports;
185185
- `controllerAs` is a controller alias name, if present the controller will be published to scope under the
186186
controllerAs name;

src/route-segment.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,25 @@ mod.provider( '$routeSegment',
359359
locals[key] = angular.isString(value) ? $injector.get(value) : $injector.invoke(value);
360360
});
361361

362-
if(params.template)
362+
if(params.template) {
363+
363364
locals.$template = params.template;
365+
if(angular.isFunction(locals.$template))
366+
locals.$template = $injector.invoke(locals.$template);
367+
}
364368

365-
if(options.autoLoadTemplates && params.templateUrl)
366-
locals.$template =
367-
$http.get(params.templateUrl, {cache: $templateCache})
368-
.then(function(response) {
369+
if(options.autoLoadTemplates && params.templateUrl) {
370+
371+
locals.$template = params.templateUrl;
372+
if(angular.isFunction(locals.$template))
373+
locals.$template = $injector.invoke(locals.$template);
374+
375+
locals.$template =
376+
$http.get(locals.$template, {cache: $templateCache})
377+
.then(function (response) {
369378
return response.data;
370379
});
380+
}
371381

372382
return $q.all(locals).then(
373383

test/unit/route-segment.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,34 @@ describe('route segment', function() {
184184
expect(callback.calls[0].args[1].segment.locals.$template).toEqual('TEST');
185185
});
186186

187+
it('should work with template set as a function', function () {
188+
189+
$routeSegmentProvider.segment('section3', {
190+
template: function(injectable) { return injectable.foo }
191+
});
192+
$provide.value('injectable', {foo: 'bar'});
193+
194+
$rootScope.$broadcast('$routeChangeSuccess', {$route: {segment: 'section3'}});
195+
$rootScope.$digest();
196+
expect(callback.calls[0].args[1].segment.locals.$template).toEqual('bar');
197+
});
198+
199+
it('should work with templateUrl set as a function', function () {
200+
201+
$routeSegmentProvider.options.autoLoadTemplates = true;
202+
$routeSegmentProvider.segment('section3', {
203+
templateUrl: function(injectable) { return injectable.foo }
204+
});
205+
$provide.value('injectable', {foo: '/abc/def'});
206+
207+
$httpBackend.expectGET('/abc/def').respond(200, 'TEST');
208+
$rootScope.$broadcast('$routeChangeSuccess', {$route: {segment: 'section3'}});
209+
210+
$rootScope.$digest();
211+
$httpBackend.flush();
212+
expect(callback.calls[0].args[1].segment.locals.$template).toEqual('TEST');
213+
});
214+
187215
it('`startsWith` should work', function () {
188216
$location.path('/X-foo');
189217

0 commit comments

Comments
 (0)