diff --git a/src/urlMatcherFactory.js b/src/urlMatcherFactory.js index 1870d2341..da17fd2c8 100644 --- a/src/urlMatcherFactory.js +++ b/src/urlMatcherFactory.js @@ -79,7 +79,7 @@ function UrlMatcher(pattern, caseInsensitiveMatch) { function addParameter(id, type) { if (!/^\w+(-+\w+)*$/.test(id)) throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern + "'"); if (params[id]) throw new Error("Duplicate parameter name '" + id + "' in pattern '" + pattern + "'"); - params[id] = type; + params[id] = angular.isNumber(type) ? new Type() : type; } function quoteRegExp(string) { @@ -238,7 +238,7 @@ UrlMatcher.prototype.validates = function (params) { result = result && self.params[key].is(val); }); return result; -} +}; /** * @ngdoc function diff --git a/test/stateSpec.js b/test/stateSpec.js index e9036f835..9fa432c9c 100644 --- a/test/stateSpec.js +++ b/test/stateSpec.js @@ -91,6 +91,9 @@ describe('state', function () { } } }) + .state('badParam', { + url: "/bad/{param:int}" + }) .state('first', { url: '^/first/subpath' }) .state('second', { url: '^/second' }) @@ -712,6 +715,7 @@ describe('state', function () { 'about.person.item', 'about.sidebar', 'about.sidebar.item', + 'badParam', 'dynamicController', 'first', 'home', @@ -779,6 +783,29 @@ describe('state', function () { expect($state.current.name).toBe(''); })); + describe("typed parameter handling", function() { + + it('should initialize parameters without a hacky empty test', inject(function ($urlMatcherFactory, $state) { + new UrlMatcher(""); + })); + + it('should ignore bad url parameters', inject(function ($state, $rootScope, $location, $urlMatcherFactory) { + $location.path("/bad/5"); + $rootScope.$broadcast("$locationChangeSuccess"); + $rootScope.$apply(); + expect($state.current.name).toBe("badParam"); + + $state.transitionTo("about"); + $rootScope.$apply(); + expect($state.current.name).toBe('about'); + + $location.path("/bad/foo"); + $rootScope.$broadcast("$locationChangeSuccess"); + $rootScope.$apply(); + expect($state.current.name).toBe("about"); + })); + }); + it('should revert to last known working url on state change failure', inject(function ($state, $rootScope, $location, $q) { $state.transitionTo("about"); $q.flush();