Skip to content

Commit 9474cc1

Browse files
committed
fix(directive): reapply ng-class when interpolated class attribute changes
Closes angular#1016
1 parent e7e438f commit 9474cc1

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

src/ng/compile.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -999,9 +999,7 @@ function $CompileProvider($provide) {
999999
// we define observers array only for interpolated attrs
10001000
// and ignore observers for non interpolated attrs to save some memory
10011001
attr.$$observers[name] = [];
1002-
if (name !== 'class') {
1003-
attr[name] = undefined;
1004-
}
1002+
attr[name] = undefined;
10051003
scope.$watch(interpolateFn, function(value) {
10061004
attr.$set(name, value);
10071005
});

src/ng/directive/ngClass.js

+18-26
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,27 @@
22

33
function classDirective(name, selector) {
44
name = 'ngClass' + name;
5-
return ['$interpolate', function($interpolate) {
6-
return function(scope, element, attr) {
7-
// Reusable function for re-applying the ngClass
8-
function reapply(newVal, oldVal) {
9-
if (selector === true || scope.$index % 2 === selector) {
10-
if (oldVal && (newVal !== oldVal)) {
11-
if (isObject(oldVal) && !isArray(oldVal))
12-
oldVal = map(oldVal, function(v, k) { if (v) return k });
13-
element.removeClass(isArray(oldVal) ? oldVal.join(' ') : oldVal);
14-
}
15-
if (isObject(newVal) && !isArray(newVal))
16-
newVal = map(newVal, function(v, k) { if (v) return k });
17-
if (newVal) element.addClass(isArray(newVal) ? newVal.join(' ') : newVal);
5+
return ngDirective(function(scope, element, attr) {
6+
// Reusable function for re-applying the ngClass
7+
function reapply(newVal, oldVal) {
8+
if (selector === true || scope.$index % 2 === selector) {
9+
if (oldVal && (newVal !== oldVal)) {
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);
1813
}
19-
};
20-
scope.$watch(attr[name], reapply, true);
21-
22-
// Watch class attribute for changes
23-
if (attr['class']) {
24-
var interpolateFn = $interpolate(attr['class'], true);
25-
scope.$watch(interpolateFn, function(newClass, oldClass) {
26-
// Reapply ngClass
27-
var ngClass = scope.$eval(attr[name]);
28-
reapply(ngClass, ngClass);
29-
});
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);
3017
}
31-
3218
};
33-
}];
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+
});
25+
});
3426
}
3527

3628
/**

0 commit comments

Comments
 (0)