4
4
*
5
5
* @requires ui.router.router.$urlRouterProvider
6
6
* @requires ui.router.util.$urlMatcherFactoryProvider
7
- * @requires $locationProvider
8
7
*
9
8
* @description
10
9
* The new `$stateProvider` works similar to Angular's v1 router, but it focuses purely
20
19
*
21
20
* The `$stateProvider` provides interfaces to declare these states for your app.
22
21
*/
23
- $StateProvider . $inject = [ '$urlRouterProvider' , '$urlMatcherFactoryProvider' , '$locationProvider' ] ;
24
- function $StateProvider ( $urlRouterProvider , $urlMatcherFactory , $locationProvider ) {
22
+ $StateProvider . $inject = [ '$urlRouterProvider' , '$urlMatcherFactoryProvider' ] ;
23
+ function $StateProvider ( $urlRouterProvider , $urlMatcherFactory ) {
25
24
26
25
var root , states = { } , $state , queue = { } , abstractKey = 'abstract' ;
27
26
@@ -521,6 +520,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
521
520
* @requires $injector
522
521
* @requires ui.router.util.$resolve
523
522
* @requires ui.router.state.$stateParams
523
+ * @requires ui.router.router.$urlRouter
524
524
*
525
525
* @property {object } params A param object, e.g. {sectionId: section.id)}, that
526
526
* you'd like to test against the current active state.
@@ -534,24 +534,14 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
534
534
* between them. It also provides interfaces to ask for current state or even states
535
535
* you're coming from.
536
536
*/
537
- // $urlRouter is injected just to ensure it gets instantiated
538
537
this . $get = $get ;
539
- $get . $inject = [ '$rootScope' , '$q' , '$view' , '$injector' , '$resolve' , '$stateParams' , '$location' , '$ urlRouter' , '$browser '] ;
540
- function $get ( $rootScope , $q , $view , $injector , $resolve , $stateParams , $location , $ urlRouter, $browser ) {
538
+ $get . $inject = [ '$rootScope' , '$q' , '$view' , '$injector' , '$resolve' , '$stateParams' , '$urlRouter' ] ;
539
+ function $get ( $rootScope , $q , $view , $injector , $resolve , $stateParams , $urlRouter ) {
541
540
542
541
var TransitionSuperseded = $q . reject ( new Error ( 'transition superseded' ) ) ;
543
542
var TransitionPrevented = $q . reject ( new Error ( 'transition prevented' ) ) ;
544
543
var TransitionAborted = $q . reject ( new Error ( 'transition aborted' ) ) ;
545
544
var TransitionFailed = $q . reject ( new Error ( 'transition failed' ) ) ;
546
- var currentLocation = $location . url ( ) ;
547
- var baseHref = $browser . baseHref ( ) ;
548
-
549
- function syncUrl ( ) {
550
- if ( $location . url ( ) !== currentLocation ) {
551
- $location . url ( currentLocation ) ;
552
- $location . replace ( ) ;
553
- }
554
- }
555
545
556
546
// Handles the case where a state which is the target of a transition is not found, and the user
557
547
// can optionally retry or defer the transition
@@ -591,7 +581,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
591
581
var evt = $rootScope . $broadcast ( '$stateNotFound' , redirect , state , params ) ;
592
582
593
583
if ( evt . defaultPrevented ) {
594
- syncUrl ( ) ;
584
+ $urlRouter . update ( ) ;
595
585
return TransitionAborted ;
596
586
}
597
587
@@ -601,7 +591,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
601
591
602
592
// Allow the handler to return a promise to defer state lookup retry
603
593
if ( options . $retry ) {
604
- syncUrl ( ) ;
594
+ $urlRouter . update ( ) ;
605
595
return TransitionFailed ;
606
596
}
607
597
var retryTransition = $state . transition = $q . when ( evt . retry ) ;
@@ -613,7 +603,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
613
603
} , function ( ) {
614
604
return TransitionAborted ;
615
605
} ) ;
616
- syncUrl ( ) ;
606
+ $urlRouter . update ( ) ;
617
607
618
608
return retryTransition ;
619
609
}
@@ -813,11 +803,11 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
813
803
814
804
// If we're going to the same state and all locals are kept, we've got nothing to do.
815
805
// But clear 'transition', as we still want to cancel any other pending transitions.
816
- // TODO: We may not want to bump 'transition' if we're called from a location change that we've initiated ourselves,
817
- // because we might accidentally abort a legitimate transition initiated from code?
806
+ // TODO: We may not want to bump 'transition' if we're called from a location change
807
+ // that we've initiated ourselves, because we might accidentally abort a legitimate
808
+ // transition initiated from code?
818
809
if ( shouldTriggerReload ( to , from , locals , options ) ) {
819
- if ( to . self . reloadOnSearch !== false )
820
- syncUrl ( ) ;
810
+ if ( to . self . reloadOnSearch !== false ) $urlRouter . update ( ) ;
821
811
$state . transition = null ;
822
812
return $q . when ( $state . current ) ;
823
813
}
@@ -855,7 +845,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
855
845
* </pre>
856
846
*/
857
847
if ( $rootScope . $broadcast ( '$stateChangeStart' , to . self , toParams , from . self , fromParams ) . defaultPrevented ) {
858
- syncUrl ( ) ;
848
+ $urlRouter . update ( ) ;
859
849
return TransitionPrevented ;
860
850
}
861
851
}
@@ -910,14 +900,10 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
910
900
copy ( $state . params , $stateParams ) ;
911
901
$state . transition = null ;
912
902
913
- // Update $location
914
- var toNav = to . navigable ;
915
- if ( options . location && toNav ) {
916
- $location . url ( toNav . url . format ( toNav . locals . globals . $stateParams ) ) ;
917
-
918
- if ( options . location === 'replace' ) {
919
- $location . replace ( ) ;
920
- }
903
+ if ( options . location && to . navigable ) {
904
+ $urlRouter . push ( to . navigable . url , to . navigable . locals . globals . $stateParams , {
905
+ replace : options . location === 'replace'
906
+ } ) ;
921
907
}
922
908
923
909
if ( options . notify ) {
@@ -937,7 +923,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
937
923
*/
938
924
$rootScope . $broadcast ( '$stateChangeSuccess' , to . self , toParams , from . self , fromParams ) ;
939
925
}
940
- currentLocation = $location . url ( ) ;
926
+ $urlRouter . update ( true ) ;
941
927
942
928
return $state . current ;
943
929
} , function ( error ) {
@@ -965,7 +951,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
965
951
evt = $rootScope . $broadcast ( '$stateChangeError' , to . self , toParams , from . self , fromParams , error ) ;
966
952
967
953
if ( ! evt . defaultPrevented ) {
968
- syncUrl ( ) ;
954
+ $urlRouter . update ( ) ;
969
955
}
970
956
971
957
return $q . reject ( error ) ;
@@ -1112,33 +1098,18 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
1112
1098
*/
1113
1099
$state . href = function href ( stateOrName , params , options ) {
1114
1100
options = extend ( { lossy : true , inherit : false , absolute : false , relative : $state . $current } , options || { } ) ;
1101
+
1115
1102
var state = findState ( stateOrName , options . relative ) ;
1116
- if ( ! isDefined ( state ) ) return null ;
1117
1103
1104
+ if ( ! isDefined ( state ) ) return null ;
1118
1105
if ( options . inherit ) params = inheritParams ( $stateParams , params || { } , $state . $current , state ) ;
1119
1106
1120
1107
var nav = ( state && options . lossy ) ? state . navigable : state ;
1121
- var url = ( nav && nav . url ) ? nav . url . format ( normalize ( state . params , params || { } ) ) : null ;
1122
- if ( ! $locationProvider . html5Mode ( ) && url ) {
1123
- url = "#" + $locationProvider . hashPrefix ( ) + url ;
1124
- }
1125
-
1126
- if ( baseHref !== '/' ) {
1127
- if ( $locationProvider . html5Mode ( ) ) {
1128
- url = baseHref . slice ( 0 , - 1 ) + url ;
1129
- } else if ( options . absolute ) {
1130
- url = baseHref . slice ( 1 ) + url ;
1131
- }
1132
- }
1133
1108
1134
- if ( options . absolute && url ) {
1135
- url = $location . protocol ( ) + '://' +
1136
- $location . host ( ) +
1137
- ( $location . port ( ) == 80 || $location . port ( ) == 443 ? '' : ':' + $location . port ( ) ) +
1138
- ( ! $locationProvider . html5Mode ( ) && url ? '/' : '' ) +
1139
- url ;
1109
+ if ( ! nav || ! nav . url ) {
1110
+ return null ;
1140
1111
}
1141
- return url ;
1112
+ return $urlRouter . href ( nav . url , normalize ( state . params , params || { } ) , { absolute : options . absolute } ) ;
1142
1113
} ;
1143
1114
1144
1115
/**
0 commit comments