Skip to content

Commit b6fe3fe

Browse files
committed
feat($stateSearch): show $location.search for current state
try to resolve angular-ui#115.
1 parent 539e207 commit b6fe3fe

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/state.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,12 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
696696
* @requires $injector
697697
* @requires ui.router.util.$resolve
698698
* @requires ui.router.state.$stateParams
699+
* @requires ui.router.state.$stateSearch
699700
* @requires ui.router.router.$urlRouter
700701
*
701-
* @property {object} params A param object, e.g. {sectionId: section.id)}, that
702+
* @property {object} params A param object, e.g. {sectionId: section.id}, that
703+
* you'd like to test against the current active state.
704+
* @property {object} search A $location.search copy, e.g. {myarg: 123}, that
702705
* you'd like to test against the current active state.
703706
* @property {object} current A reference to the state's config object. However
704707
* you passed it in. Useful for accessing custom data.
@@ -711,8 +714,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
711714
* you're coming from.
712715
*/
713716
this.$get = $get;
714-
$get.$inject = ['$rootScope', '$q', '$view', '$injector', '$resolve', '$stateParams', '$urlRouter', '$location', '$urlMatcherFactory'];
715-
function $get( $rootScope, $q, $view, $injector, $resolve, $stateParams, $urlRouter, $location, $urlMatcherFactory) {
717+
$get.$inject = ['$rootScope', '$q', '$view', '$injector', '$resolve', '$stateParams', '$stateSearch', '$urlRouter', '$location', '$urlMatcherFactory'];
718+
function $get( $rootScope, $q, $view, $injector, $resolve, $stateParams, $stateSearch, $urlRouter, $location, $urlMatcherFactory) {
716719

717720
var TransitionSuperseded = $q.reject(new Error('transition superseded'));
718721
var TransitionPrevented = $q.reject(new Error('transition prevented'));
@@ -788,6 +791,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
788791

789792
$state = {
790793
params: {},
794+
search: {},
791795
current: root.self,
792796
$current: root,
793797
transition: null
@@ -1042,6 +1046,10 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
10421046
});
10431047
$urlRouter.update(true);
10441048
}
1049+
var toSearch = angular.extend({}, $location.search());
1050+
$state.search = toSearch;
1051+
copy($state.search, $stateSearch);
1052+
10451053
$state.transition = null;
10461054
return $q.when($state.current);
10471055
}
@@ -1145,6 +1153,10 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
11451153
});
11461154
}
11471155

1156+
var toSearch = angular.extend({}, $location.search());
1157+
$state.search = toSearch;
1158+
copy($state.search, $stateSearch);
1159+
11481160
if (options.notify) {
11491161
/**
11501162
* @ngdoc event
@@ -1463,4 +1475,5 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
14631475

14641476
angular.module('ui.router.state')
14651477
.factory('$stateParams', function () { return {}; })
1478+
.factory('$stateSearch', function () { return {}; })
14661479
.provider('$state', $StateProvider);

test/stateSpec.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,21 @@ describe('state', function () {
233233
expect(called).toBeFalsy();
234234
}));
235235

236-
it('updates URL when changing only query params via $state.go() when reloadOnSearch=false', inject(function ($state, $stateParams, $q, $location, $rootScope){
236+
it('updates $stateSearch when state.reloadOnSearch=false', inject(function ($state, $stateParams, $stateSearch, $q, $location, $rootScope){
237+
initStateTo(RS);
238+
$location.search({term: 'hello', myarg: 'search'});
239+
var called;
240+
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams, options) {
241+
called = true
242+
});
243+
$q.flush();
244+
expect($stateParams).toEqual({term: 'hello'});
245+
expect($state.search).toEqual({term: 'hello', myarg: 'search'});
246+
expect($stateSearch).toEqual({term: 'hello', myarg: 'search'});
247+
expect(called).toBeFalsy();
248+
}));
249+
250+
it('updates URL when changing only query params via $state.go() when reloadOnSearch=false', inject(function ($state, $stateParams, $stateSearch, $q, $location, $rootScope){
237251
initStateTo(RS);
238252
var called;
239253
$state.go(".", { term: 'goodbye' });
@@ -242,6 +256,8 @@ describe('state', function () {
242256
});
243257
$q.flush();
244258
expect($stateParams).toEqual({term: 'goodbye'});
259+
expect($state.search).toEqual({term: 'goodbye'});
260+
expect($stateSearch).toEqual({term: 'goodbye'});
245261
expect($location.url()).toEqual("/search?term=goodbye");
246262
expect(called).toBeFalsy();
247263
}));
@@ -605,7 +621,7 @@ describe('state', function () {
605621
expect($state.$current.name).toBe('about.sidebar');
606622
}));
607623

608-
it('keeps parameters from common ancestor states', inject(function ($state, $stateParams, $q) {
624+
it('keeps parameters from common ancestor states', inject(function ($state, $stateParams, $stateSearch, $q) {
609625
$state.transitionTo('about.person', { person: 'bob' });
610626
$q.flush();
611627

@@ -614,6 +630,7 @@ describe('state', function () {
614630

615631
expect($state.$current.name).toBe('about.person.item');
616632
expect($stateParams).toEqual({ person: 'bob', id: "5" });
633+
expect($stateSearch).toEqual({});
617634

618635
$state.go('^.^.sidebar');
619636
$q.flush();

0 commit comments

Comments
 (0)