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

Commit b41fe9f

Browse files
maxmartmhevery
authored andcommitted
fix(ngClass): works with class interpolation
Closes #1016
1 parent 9b5656e commit b41fe9f

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

src/ng/directive/ngClass.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@
33
function classDirective(name, selector) {
44
name = 'ngClass' + name;
55
return ngDirective(function(scope, element, attr) {
6-
scope.$watch(attr[name], function(newVal, oldVal) {
6+
// Reusable function for re-applying the ngClass
7+
function reapply(newVal, oldVal) {
78
if (selector === true || scope.$index % 2 === selector) {
89
if (oldVal && (newVal !== oldVal)) {
9-
if (isObject(oldVal) && !isArray(oldVal))
10-
oldVal = map(oldVal, function(v, k) { if (v) return k });
11-
element.removeClass(isArray(oldVal) ? oldVal.join(' ') : oldVal);
12-
}
13-
if (isObject(newVal) && !isArray(newVal))
14-
newVal = map(newVal, function(v, k) { if (v) return k });
15-
if (newVal) element.addClass(isArray(newVal) ? newVal.join(' ') : newVal); }
16-
}, true);
10+
if (isObject(oldVal) && !isArray(oldVal))
11+
oldVal = map(oldVal, function(v, k) { if (v) return k });
12+
element.removeClass(isArray(oldVal) ? oldVal.join(' ') : oldVal);
13+
}
14+
if (isObject(newVal) && !isArray(newVal))
15+
newVal = map(newVal, function(v, k) { if (v) return k });
16+
if (newVal) element.addClass(isArray(newVal) ? newVal.join(' ') : newVal);
17+
}
18+
};
19+
scope.$watch(attr[name], reapply, true);
20+
21+
attr.$observe('class', function(value) {
22+
var ngClass = scope.$eval(attr[name]);
23+
reapply(ngClass, ngClass);
24+
});
1725
});
1826
}
1927

test/ng/directive/ngClassSpec.js

+33
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,37 @@ describe('ngClass', function() {
201201
expect(e2.hasClass('C')).toBeFalsy();
202202
expect(e2.hasClass('D')).toBeFalsy();
203203
}));
204+
205+
206+
it('should reapply ngClass when interpolated class attribute changes', inject(function($rootScope, $compile) {
207+
element = $compile('<div class="one {{cls}} three" ng-class="{four: four}"></div>')($rootScope);
208+
209+
$rootScope.$apply(function() {
210+
$rootScope.cls = "two";
211+
$rootScope.four = true;
212+
});
213+
expect(element).toHaveClass('one');
214+
expect(element).toHaveClass('two'); // interpolated
215+
expect(element).toHaveClass('three');
216+
expect(element).toHaveClass('four');
217+
218+
$rootScope.$apply(function() {
219+
$rootScope.cls = "too";
220+
});
221+
expect(element).toHaveClass('one');
222+
expect(element).toHaveClass('too'); // interpolated
223+
expect(element).toHaveClass('three');
224+
expect(element).toHaveClass('four'); // should still be there
225+
expect(element.hasClass('two')).toBeFalsy();
226+
227+
$rootScope.$apply(function() {
228+
$rootScope.cls = "to";
229+
});
230+
expect(element).toHaveClass('one');
231+
expect(element).toHaveClass('to'); // interpolated
232+
expect(element).toHaveClass('three');
233+
expect(element).toHaveClass('four'); // should still be there
234+
expect(element.hasClass('two')).toBeFalsy();
235+
expect(element.hasClass('too')).toBeFalsy();
236+
}));
204237
});

0 commit comments

Comments
 (0)