Skip to content

Commit 1fa3ebf

Browse files
committed
Merge pull request angular-ui#446 from roryf/master
Add optional parameter matching to $state.is and $state.includes
2 parents 0dbb804 + 93cd8f3 commit 1fa3ebf

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/state.js

+27-4
Original file line numberDiff line numberDiff line change
@@ -377,14 +377,37 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
377377
return transition;
378378
};
379379

380-
$state.is = function is(stateOrName) {
380+
$state.is = function is(stateOrName, params) {
381381
var state = findState(stateOrName);
382-
return (isDefined(state)) ? $state.$current === state : undefined;
382+
383+
if (!isDefined(state)) {
384+
return undefined;
385+
}
386+
387+
if ($state.$current !== state) {
388+
return false;
389+
}
390+
391+
return isDefined(params) ? angular.equals($stateParams, params) : true;
383392
};
384393

385-
$state.includes = function includes(stateOrName) {
394+
$state.includes = function includes(stateOrName, params) {
386395
var state = findState(stateOrName);
387-
return (isDefined(state)) ? isDefined($state.$current.includes[state.name]) : undefined;
396+
if (!isDefined(state)) {
397+
return undefined;
398+
}
399+
400+
if (!isDefined($state.$current.includes[state.name])) {
401+
return false;
402+
}
403+
404+
var validParams = true;
405+
angular.forEach(params, function(value, key) {
406+
if (!isDefined($stateParams[key]) || $stateParams[key] !== value) {
407+
validParams = false;
408+
}
409+
});
410+
return validParams;
388411
};
389412

390413
$state.href = function href(stateOrName, params, options) {

test/stateSpec.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,13 @@ describe('state', function () {
393393
it('should return undefined when queried state does not exist', inject(function ($state) {
394394
expect($state.is('Z')).toBeUndefined();
395395
}));
396+
397+
it('should return true when the current state is passed with matching parameters', inject(function ($state, $q) {
398+
$state.transitionTo(D, {x: 'foo', y: 'bar'}); $q.flush();
399+
expect($state.is(D, {x: 'foo', y: 'bar'})).toBe(true);
400+
expect($state.is('D', {x: 'foo', y: 'bar'})).toBe(true);
401+
expect($state.is(D, {x: 'bar', y: 'foo'})).toBe(false);
402+
}));
396403
});
397404

398405
describe('.includes()', function () {
@@ -411,7 +418,21 @@ describe('state', function () {
411418
}));
412419

413420
it('should return undefined when queried state does not exist', inject(function ($state) {
414-
expect($state.is('Z')).toBeUndefined();
421+
expect($state.includes('Z')).toBeUndefined();
422+
}));
423+
424+
it('should return true when the current state is passed with partial matching parameters', inject(function ($state, $q) {
425+
$state.transitionTo(D, {x: 'foo', y: 'bar'}); $q.flush();
426+
expect($state.includes(D, {x: 'foo'})).toBe(true);
427+
expect($state.includes(D, {y: 'bar'})).toBe(true);
428+
expect($state.includes('D', {x: 'foo'})).toBe(true);
429+
expect($state.includes(D, {y: 'foo'})).toBe(false);
430+
}));
431+
432+
it('should return true when the current state is passed with partial matching parameters from state\'s parent', inject(function ($state, $q) {
433+
$state.transitionTo('about.person.item', {person: 'bob', id: 5}); $q.flush();
434+
expect($state.includes('about.person', {person: 'bob'})).toBe(true);
435+
expect($state.includes('about.person', {person: 'steve'})).toBe(false);
415436
}));
416437
});
417438

0 commit comments

Comments
 (0)