|
3 | 3 | function classDirective(name, selector) {
|
4 | 4 | name = 'ngClass' + name;
|
5 | 5 | 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 | + |
19 | 7 | scope.$watch(attr[name], ngClassWatchAction, true);
|
20 | 8 |
|
21 | 9 | attr.$observe('class', function(value) {
|
22 | 10 | var ngClass = scope.$eval(attr[name]);
|
23 | 11 | ngClassWatchAction(ngClass, ngClass);
|
24 | 12 | });
|
| 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 | + } |
25 | 55 | });
|
26 | 56 | }
|
27 | 57 |
|
|
0 commit comments