|
3 | 3 | function classDirective(name, selector) {
|
4 | 4 | name = 'ngClass' + name;
|
5 | 5 | 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) { |
7 | 8 | if (selector === true || scope.$index % 2 === selector) {
|
8 | 9 | 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 | + }); |
17 | 25 | });
|
18 | 26 | }
|
19 | 27 |
|
|
0 commit comments