Skip to content

Commit 3f60fbe

Browse files
fix(): populate default params in .transitionTo. closes #1396
1 parent 0cc1e6c commit 3f60fbe

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/state.js

+2
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
778778
}
779779
if (toState[abstractKey]) throw new Error("Cannot transition to abstract state '" + to + "'");
780780
if (options.inherit) toParams = inheritParams($stateParams, toParams || {}, $state.$current, toState);
781+
var defaultParams = toState.params.$$values();
782+
toParams = extend(defaultParams, toParams);
781783
to = toState;
782784

783785
var toPath = to.path;

test/stateSpec.js

+28-5
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ describe('state', function () {
2020
var A = { data: {} },
2121
B = {},
2222
C = {},
23-
D = { params: { x: {}, y: {} } },
24-
DD = { parent: D, params: { x: {}, y: {}, z: {} } },
23+
D = { params: { x: null, y: null } },
24+
DD = { parent: D, params: { x: null, y: null, z: null } },
2525
E = { params: { i: {} } },
2626
H = { data: {propA: 'propA', propB: 'propB'} },
2727
HH = { parent: H },
2828
HHH = {parent: HH, data: {propA: 'overriddenA', propC: 'propC'} },
2929
RS = { url: '^/search?term', reloadOnSearch: false },
30+
OPT = { url: '/opt/:param', params: { param: 100 } },
3031
AppInjectable = {};
3132

3233
beforeEach(module(function ($stateProvider, $provide) {
@@ -46,6 +47,7 @@ describe('state', function () {
4647
.state('H', H)
4748
.state('HH', HH)
4849
.state('HHH', HHH)
50+
.state('OPT', OPT)
4951
.state('RS', RS)
5052

5153
.state('home', { url: "/" })
@@ -101,8 +103,8 @@ describe('state', function () {
101103
// State param inheritance tests. param1 is inherited by sub1 & sub2;
102104
// param2 should not be transferred (unless explicitly set).
103105
.state('root', { url: '^/root?param1' })
104-
.state('root.sub1', {url: '/1?param2' })
105-
.state('root.sub2', {url: '/2?param2' });
106+
.state('root.sub1', {url: '/1?param2' });
107+
$stateProvider.state('root.sub2', {url: '/2?param2' });
106108

107109
$provide.value('AppInjectable', AppInjectable);
108110
}));
@@ -642,7 +644,7 @@ describe('state', function () {
642644

643645
it('contains the parameter values for the current state', inject(function ($state, $q) {
644646
initStateTo(D, { x: 'x value', z: 'invalid value' });
645-
expect($state.params).toEqual({ x: 'x value', y: undefined });
647+
expect($state.params).toEqual({ x: 'x value', y: null });
646648
}));
647649
});
648650

@@ -745,6 +747,7 @@ describe('state', function () {
745747
'H',
746748
'HH',
747749
'HHH',
750+
'OPT',
748751
'RS',
749752
'about',
750753
'about.person',
@@ -786,6 +789,26 @@ describe('state', function () {
786789
}));
787790
});
788791

792+
describe('optional parameters', function() {
793+
it("should be populated during transition, if unspecified", inject(function($state, $q) {
794+
var stateParams;
795+
$state.get("OPT").onEnter = function($stateParams) { stateParams = $stateParams; };
796+
$state.go("OPT"); $q.flush();
797+
expect($state.current.name).toBe("OPT");
798+
expect($state.params).toEqual({ param: 100 });
799+
expect(stateParams).toEqual({ param: 100 });
800+
}));
801+
802+
it("should be populated during primary transition, if unspecified", inject(function($state, $q) {
803+
var count = 0;
804+
$state.get("OPT").onEnter = function($stateParams) { count++; };
805+
$state.go("OPT"); $q.flush();
806+
expect($state.current.name).toBe("OPT");
807+
expect($state.params).toEqual({ param: 100 });
808+
expect(count).toEqual(1);
809+
}));
810+
});
811+
789812
describe('url handling', function () {
790813
it('should transition to the same state with different parameters', inject(function ($state, $rootScope, $location) {
791814
$location.path("/about/bob");

0 commit comments

Comments
 (0)