From ef09517061b0b4c0c9e9f85086635af33207ec54 Mon Sep 17 00:00:00 2001 From: Chris Chua Date: Wed, 1 Oct 2014 00:36:01 -0700 Subject: [PATCH] fix(dropdown): compatibility with $location url rewriting Fixes #2343 --- src/dropdown/dropdown.js | 4 ++++ src/dropdown/test/dropdown.spec.js | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/dropdown/dropdown.js b/src/dropdown/dropdown.js index 42b36bef42..e01c830847 100644 --- a/src/dropdown/dropdown.js +++ b/src/dropdown/dropdown.js @@ -29,6 +29,10 @@ angular.module('ui.bootstrap.dropdown', []) }; var closeDropdown = function( evt ) { + // This method may still be called during the same mouse event that + // unbound this event handler. So check openScope before proceeding. + if (!openScope) { return; } + var toggleElement = openScope.getToggleElement(); if ( evt && toggleElement && toggleElement[0].contains(evt.target) ) { return; diff --git a/src/dropdown/test/dropdown.spec.js b/src/dropdown/test/dropdown.spec.js index 2bc379708a..262549aeea 100644 --- a/src/dropdown/test/dropdown.spec.js +++ b/src/dropdown/test/dropdown.spec.js @@ -168,6 +168,33 @@ describe('dropdownToggle', function() { }); }); + describe('integration with $location URL rewriting', function() { + function dropdown() { + + // Simulate URL rewriting behavior + $document.on('click', 'a[href="#something"]', function () { + $rootScope.$broadcast('$locationChangeSuccess'); + $rootScope.$apply(); + }); + + return $compile('
  • ' + + '
  • ')($rootScope); + } + + beforeEach(function() { + element = dropdown(); + }); + + it('should close without errors on $location change', function() { + $document.find('body').append(element); + clickDropdownToggle(); + expect(element.hasClass('open')).toBe(true); + var optionEl = element.find('ul > li').eq(0).find('a').eq(0); + optionEl.click(); + expect(element.hasClass('open')).toBe(false); + }); + }); + describe('without trigger', function() { beforeEach(function() { $rootScope.isopen = true;