Skip to content

feat($stateSearch): show $location.search for current state #2383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,9 +696,12 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
* @requires $injector
* @requires ui.router.util.$resolve
* @requires ui.router.state.$stateParams
* @requires ui.router.state.$stateSearch
* @requires ui.router.router.$urlRouter
*
* @property {object} params A param object, e.g. {sectionId: section.id)}, that
* @property {object} params A param object, e.g. {sectionId: section.id}, that
* you'd like to test against the current active state.
* @property {object} search A $location.search copy, e.g. {myarg: 123}, that
* you'd like to test against the current active state.
* @property {object} current A reference to the state's config object. However
* you passed it in. Useful for accessing custom data.
Expand All @@ -711,8 +714,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
* you're coming from.
*/
this.$get = $get;
$get.$inject = ['$rootScope', '$q', '$view', '$injector', '$resolve', '$stateParams', '$urlRouter', '$location', '$urlMatcherFactory'];
function $get( $rootScope, $q, $view, $injector, $resolve, $stateParams, $urlRouter, $location, $urlMatcherFactory) {
$get.$inject = ['$rootScope', '$q', '$view', '$injector', '$resolve', '$stateParams', '$stateSearch', '$urlRouter', '$location', '$urlMatcherFactory'];
function $get( $rootScope, $q, $view, $injector, $resolve, $stateParams, $stateSearch, $urlRouter, $location, $urlMatcherFactory) {

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

$state = {
params: {},
search: {},
current: root.self,
$current: root,
transition: null
Expand Down Expand Up @@ -1042,6 +1046,10 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
});
$urlRouter.update(true);
}
var toSearch = angular.extend({}, $location.search());
$state.search = toSearch;
copy($state.search, $stateSearch);

$state.transition = null;
return $q.when($state.current);
}
Expand Down Expand Up @@ -1145,6 +1153,10 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
});
}

var toSearch = angular.extend({}, $location.search());
$state.search = toSearch;
copy($state.search, $stateSearch);

if (options.notify) {
/**
* @ngdoc event
Expand Down Expand Up @@ -1463,4 +1475,5 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {

angular.module('ui.router.state')
.factory('$stateParams', function () { return {}; })
.factory('$stateSearch', function () { return {}; })
.provider('$state', $StateProvider);
21 changes: 19 additions & 2 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,21 @@ describe('state', function () {
expect(called).toBeFalsy();
}));

it('updates URL when changing only query params via $state.go() when reloadOnSearch=false', inject(function ($state, $stateParams, $q, $location, $rootScope){
it('updates $stateSearch when state.reloadOnSearch=false', inject(function ($state, $stateParams, $stateSearch, $q, $location, $rootScope){
initStateTo(RS);
$location.search({term: 'hello', myarg: 'search'});
var called;
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams, options) {
called = true
});
$q.flush();
expect($stateParams).toEqual({term: 'hello'});
expect($state.search).toEqual({term: 'hello', myarg: 'search'});
expect($stateSearch).toEqual({term: 'hello', myarg: 'search'});
expect(called).toBeFalsy();
}));

it('updates URL when changing only query params via $state.go() when reloadOnSearch=false', inject(function ($state, $stateParams, $stateSearch, $q, $location, $rootScope){
initStateTo(RS);
var called;
$state.go(".", { term: 'goodbye' });
Expand All @@ -242,6 +256,8 @@ describe('state', function () {
});
$q.flush();
expect($stateParams).toEqual({term: 'goodbye'});
expect($state.search).toEqual({term: 'goodbye'});
expect($stateSearch).toEqual({term: 'goodbye'});
expect($location.url()).toEqual("/search?term=goodbye");
expect(called).toBeFalsy();
}));
Expand Down Expand Up @@ -605,7 +621,7 @@ describe('state', function () {
expect($state.$current.name).toBe('about.sidebar');
}));

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

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

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

$state.go('^.^.sidebar');
$q.flush();
Expand Down