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

Commit d859dce

Browse files
committed
fix(ngClassOdd/ngClassEven): support shrinking/reordering in repeaters
We need to watch $index in addition to cssClasses because only then we can correctly respond to shrinking or reordered repeaters. Closes #1076
1 parent d3b32a7 commit d859dce

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

src/ng/directive/ngClass.js

+43-13
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,55 @@
33
function classDirective(name, selector) {
44
name = 'ngClass' + name;
55
return ngDirective(function(scope, element, attr) {
6-
// Reusable function for re-applying the ngClass
7-
function ngClassWatchAction(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);
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-
};
6+
197
scope.$watch(attr[name], ngClassWatchAction, true);
208

219
attr.$observe('class', function(value) {
2210
var ngClass = scope.$eval(attr[name]);
2311
ngClassWatchAction(ngClass, ngClass);
2412
});
13+
14+
15+
if (name !== 'ngClass') {
16+
scope.$watch('$index', function($index, old$index) {
17+
var mod = $index % 2;
18+
if (mod !== old$index % 2) {
19+
if (mod == selector) {
20+
addClass(scope.$eval(attr[name]));
21+
} else {
22+
removeClass(scope.$eval(attr[name]));
23+
}
24+
}
25+
});
26+
}
27+
28+
29+
function ngClassWatchAction(newVal, oldVal) {
30+
if (selector === true || scope.$index % 2 === selector) {
31+
if (oldVal && (newVal !== oldVal)) {
32+
removeClass(oldVal);
33+
}
34+
addClass(newVal);
35+
}
36+
}
37+
38+
39+
function removeClass(classVal) {
40+
if (isObject(classVal) && !isArray(classVal)) {
41+
classVal = map(classVal, function(v, k) { if (v) return k });
42+
}
43+
element.removeClass(isArray(classVal) ? classVal.join(' ') : classVal);
44+
}
45+
46+
47+
function addClass(classVal) {
48+
if (isObject(classVal) && !isArray(classVal)) {
49+
classVal = map(classVal, function(v, k) { if (v) return k });
50+
}
51+
if (classVal) {
52+
element.addClass(isArray(classVal) ? classVal.join(' ') : classVal);
53+
}
54+
}
2555
});
2656
}
2757

0 commit comments

Comments
 (0)