diff --git a/src/ng/location.js b/src/ng/location.js index 727a0c2beddd..cf50952d53c3 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -550,8 +550,8 @@ function $LocationProvider(){ // traverse the DOM up to find first A tag while (lowercase(elm[0].nodeName) !== 'a') { - if (elm[0] === $rootElement[0]) return; - elm = elm.parent(); + // ignore rewriting if no A tag (reached root element, or no parent - removed from document) + if (elm[0] === $rootElement[0] || !(elm = elm.parent())[0]) return; } var absHref = elm.prop('href'), diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index 88747dff1cb1..cdad1f1f1b41 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -1087,6 +1087,21 @@ describe('$location', function() { expect(event.preventDefault).not.toHaveBeenCalled(); }); }); + + + // regression https://github.com/angular/angular.js/issues/1058 + it('should not throw if element was removed', inject(function($document, $rootElement, $location) { + // we need to do this otherwise we can't simulate events + $document.find('body').append($rootElement); + + $rootElement.html(''); + var button = $rootElement.find('button'); + + button.bind('click', function() { + button.remove(); + }); + browserTrigger(button, 'click'); + })); });