Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 33d45d8

Browse files
matskomhevery
authored andcommitted
fix(ngClass): ensure ngClass doesn't fire addClass or removeClass with an empty string
If ngClass fires off an add- or removeClass whilst the opposite animation is going on then the animation will be skipped. The default behavior of ngClass was executing remoteClass with an empty string while addClass had just fired. This commit fixes that bug.
1 parent 419ed04 commit 33d45d8

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/ng/directive/ngClass.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,18 @@ function classDirective(name, selector) {
4242

4343

4444
function removeClass(classVal) {
45-
$animate.removeClass(element, flattenClasses(classVal));
45+
classVal = flattenClasses(classVal);
46+
if(classVal && classVal.length > 0) {
47+
$animate.removeClass(element, classVal);
48+
}
4649
}
4750

4851

4952
function addClass(classVal) {
50-
$animate.addClass(element, flattenClasses(classVal));
53+
classVal = flattenClasses(classVal);
54+
if(classVal && classVal.length > 0) {
55+
$animate.addClass(element, classVal);
56+
}
5157
}
5258

5359
function flattenClasses(classVal) {

test/ng/directive/ngClassSpec.js

+43
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,46 @@ describe('ngClass', function() {
304304
expect(e2.hasClass('odd')).toBeFalsy();
305305
}));
306306
});
307+
308+
describe('ngClass animations', function() {
309+
var body, element, $rootElement;
310+
311+
beforeEach(module('mock.animate'));
312+
313+
it("should avoid calling addClass accidentally when removeClass is going on",
314+
inject(function($compile, $rootScope, $animate, $timeout) {
315+
316+
var element = angular.element('<div ng-class="val"></div>');
317+
var body = jqLite(document.body);
318+
body.append(element);
319+
$compile(element)($rootScope);
320+
321+
expect($animate.queue.length).toBe(0);
322+
323+
$rootScope.val = 'one';
324+
$rootScope.$digest();
325+
$animate.process('addClass');
326+
$animate.process('addClass');
327+
$timeout.flush();
328+
expect($animate.queue.length).toBe(0);
329+
330+
$rootScope.val = '';
331+
$rootScope.$digest();
332+
$animate.process('removeClass'); //only removeClass is called
333+
expect($animate.queue.length).toBe(0);
334+
$timeout.flush();
335+
336+
$rootScope.val = 'one';
337+
$rootScope.$digest();
338+
$animate.process('addClass');
339+
$timeout.flush();
340+
expect($animate.queue.length).toBe(0);
341+
342+
$rootScope.val = 'two';
343+
$rootScope.$digest();
344+
$animate.process('removeClass');
345+
$animate.process('addClass');
346+
$timeout.flush();
347+
expect($animate.queue.length).toBe(0);
348+
}));
349+
});

0 commit comments

Comments
 (0)