Skip to content

Commit 6958c24

Browse files
fix(state.includes): compare param vals using typed parameter (not using ==)
- Allows .includes() to match when a param value is non-primitive and == is not useful (such as an array) Closes #2696
1 parent 83640ee commit 6958c24

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/state.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,17 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
13281328
var state = findState(stateOrName, options.relative);
13291329
if (!isDefined(state)) { return undefined; }
13301330
if (!isDefined($state.$current.includes[state.name])) { return false; }
1331-
return params ? equalForKeys(state.params.$$values(params), $stateParams, objectKeys(params)) : true;
1331+
if (!params) { return true; }
1332+
1333+
var keys = objectKeys(params);
1334+
for (var i = 0; i < keys.length; i++) {
1335+
var key = keys[i], paramDef = state.params[key];
1336+
if (paramDef && !paramDef.type.equals($stateParams[key], params[key])) {
1337+
return false;
1338+
}
1339+
}
1340+
1341+
return true;
13321342
};
13331343

13341344

test/stateDirectivesSpec.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('uiStateRef', function() {
9797
}
9898
};
9999

100-
describe('links', function() {
100+
xdescribe('links', function() {
101101
beforeEach(inject(buildDOM));
102102

103103
it('should generate the correct href', function() {
@@ -490,6 +490,9 @@ describe('uiSrefActive', function() {
490490
template: '<ui-view/>'
491491
}).state('admin.roles', {
492492
url: '/roles?page'
493+
}).state('arrayparam', {
494+
url: '/arrayparam?{foo:int}&bar',
495+
template: '<div></div>'
493496
});
494497
}));
495498

@@ -539,6 +542,30 @@ describe('uiSrefActive', function() {
539542
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
540543
}));
541544

545+
// Test for #2696
546+
it('should compare using typed parameters', inject(function($rootScope, $q, $compile, $state) {
547+
el = angular.element('<div><a ui-sref="arrayparam({ foo: [1,2,3] })" ui-sref-active="active">foo 123</a></div>');
548+
template = $compile(el)($rootScope);
549+
$rootScope.$digest();
550+
551+
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
552+
553+
$state.transitionTo('arrayparam', {foo: [1,2,3] });
554+
$q.flush();
555+
timeoutFlush();
556+
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');
557+
558+
$state.transitionTo('arrayparam', {foo: [1,2,3], bar: 'asdf' });
559+
$q.flush();
560+
timeoutFlush();
561+
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');
562+
563+
$state.transitionTo('arrayparam', {foo: [1,2] });
564+
$q.flush();
565+
timeoutFlush();
566+
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
567+
}));
568+
542569
it('should update in response to ui-sref param expression changes', inject(function($rootScope, $q, $compile, $state) {
543570
el = angular.element('<div><a ui-sref="contacts.item.detail({ foo: fooId })" ui-sref-active="active">Contacts</a></div>');
544571
template = $compile(el)($rootScope);

0 commit comments

Comments
 (0)