Skip to content

Commit 8d20b20

Browse files
committed
Using correctly-scoped parameters for templateUrl(), fixes #312
1 parent a2ae389 commit 8d20b20

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

src/state.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
377377

378378
// Template
379379
promises.push($q
380-
.when($view.load(name, { view: view, locals: locals, notify: false }) || '')
380+
.when($view.load(name, { view: view, locals: locals, params: $stateParams, notify: false }) || '')
381381
.then(function (result) {
382382
_$view.$template = result;
383383
}));

src/view.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ $ViewProvider.$inject = [];
33
function $ViewProvider() {
44

55
this.$get = $get;
6-
$get.$inject = ['$rootScope', '$templateFactory', '$stateParams'];
7-
function $get( $rootScope, $templateFactory, $stateParams) {
6+
$get.$inject = ['$rootScope', '$templateFactory'];
7+
function $get( $rootScope, $templateFactory) {
88
return {
9-
// $view.load('full.viewName', { template: ..., controller: ..., resolve: ..., async: false })
9+
// $view.load('full.viewName', { template: ..., controller: ..., resolve: ..., async: false, params: ... })
1010
load: function load(name, options) {
11-
var result;
12-
options = extend({
13-
template: null, controller: null, view: null, locals: null, notify: true, async: true
14-
}, options);
15-
11+
var result, defaults = {
12+
template: null, controller: null, view: null, locals: null, notify: true, async: true, params: {}
13+
};
14+
options = extend(defaults, options);
15+
1616
if (options.view) {
17-
result = $templateFactory.fromConfig(options.view, $stateParams, options.locals);
17+
result = $templateFactory.fromConfig(options.view, options.params, options.locals);
1818
}
1919
if (result && options.notify) {
2020
$rootScope.$broadcast('$viewContentLoading', options);

test/stateSpec.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
describe('state', function () {
22

3-
var locationProvider;
3+
var locationProvider, templateParams;
44

55
beforeEach(module('ui.router', function($locationProvider) {
66
locationProvider = $locationProvider;
@@ -50,7 +50,14 @@ describe('state', function () {
5050
.state('about', { url: "/about" })
5151
.state('about.person', { url: "/:person" })
5252
.state('about.person.item', { url: "/:id" })
53-
.state('about.sidebar', {});
53+
.state('about.sidebar', {})
54+
.state('about.sidebar.item', {
55+
url: "/:item",
56+
templateUrl: function(params) {
57+
templateParams = params;
58+
return "/templates/" + params.item + ".html";
59+
}
60+
});
5461

5562
$provide.value('AppInjectable', AppInjectable);
5663
}));
@@ -442,8 +449,7 @@ describe('state', function () {
442449

443450
describe('default properties', function () {
444451
it('should always have a name', inject(function ($state, $q) {
445-
$state.transitionTo(A);
446-
$q.flush();
452+
$state.transitionTo(A); $q.flush();
447453
expect($state.$current.name).toBe('A');
448454
expect($state.$current.toString()).toBe('A');
449455
}));
@@ -453,9 +459,16 @@ describe('state', function () {
453459
}));
454460

455461
it('should include itself and parent states', inject(function ($state, $q) {
456-
$state.transitionTo(DD);
457-
$q.flush();
462+
$state.transitionTo(DD); $q.flush();
458463
expect($state.$current.includes).toEqual({ '': true, D: true, DD: true });
459464
}));
460465
});
466+
467+
describe('template handling', function () {
468+
it('should inject $stateParams into templateUrl function', inject(function ($state, $q, $httpBackend) {
469+
$httpBackend.expectGET("/templates/foo.html").respond("200");
470+
$state.transitionTo('about.sidebar.item', { item: "foo" }); $q.flush();
471+
expect(templateParams).toEqual({ item: "foo" });
472+
}));
473+
});
461474
});

0 commit comments

Comments
 (0)