Skip to content

Commit 71aaae9

Browse files
committed
Changed configuration to be done via the abstract setting. Added support for using function to specify child state.
1 parent b6cc66f commit 71aaae9

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ var app = angular.module('myApp', ['ng', 'ui.router.default']);
2424
Defining Default Child State
2525
----------------------------
2626

27-
In you state definition for an abstract state, add a `default` string property with the (relative or absolute) name of a child state. When a state transtion targets this abstract state, it will be redirected to the default child state instead.
27+
In your state definition for an abstract state, set the `abstract` property to the name of a child state (relative or absolute).
28+
The child state name can be provided statically as a string dynamically as a function callback.
29+
30+
When a state transtion targets this abstract state, it will be redirected to the default child state instead.
2831

2932
```javascript
3033
$stateProvider
3134
.state('parent', {
32-
abstract: true,
33-
default: '.index',
35+
abstract: '.index',
3436
template: '<ui-view/>'
3537
})
3638
.state('parent.index', {
@@ -39,6 +41,18 @@ $stateProvider
3941
.state('parent.page2', {
4042
...
4143
})
44+
.state('another', {
45+
abstract: ['$rootScope', function($rootScope) {
46+
return $rootScope.edit ? '.edit' : '.display';
47+
}]
48+
})
49+
.state('another.display', {
50+
...
51+
})
52+
.state('another.edit', {
53+
...
54+
})
55+
)
4256
```
4357

4458
Using Default Child State

angular-ui-router-default.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
var max_redirects = 10;
44
angular.module('ui.router.default', ['ui.router'])
55
.config(['$provide', function($provide) {
6-
$provide.decorator('$state', ['$delegate', function($delegate) {
6+
$provide.decorator('$state', ['$delegate', '$injector', function($delegate, $injector) {
77
var transitionTo = $delegate.transitionTo;
88
$delegate.transitionTo = function(to, toParams, options) {
99
var numRedirects = 0;
1010
while(numRedirects++ < max_redirects) {
1111
var target = this.get(to, this.$current);
12-
if(target.abstract && target.default) {
13-
if(target.default[0] == '.') {
14-
to += target.default;
12+
if(target.abstract && target.abstract !== true) {
13+
var childState = target.abstract;
14+
if(!angular.isString(childState)) {
15+
childState = $injector.invoke(childState);
16+
}
17+
if(childState[0] == '.') {
18+
to += childState;
1519
} else {
16-
to = target.default;
20+
to = childState;
1721
}
1822
} else {
1923
break;
@@ -27,4 +31,4 @@
2731
return $delegate;
2832
}])
2933
}]);
30-
})(window.angular);
34+
})(window.angular);

0 commit comments

Comments
 (0)