|
2 | 2 |
|
3 | 3 | function classDirective(name, selector) {
|
4 | 4 | name = 'ngClass' + name;
|
5 |
| - return ngDirective(function(scope, element, attr) { |
6 |
| - scope.$watch(attr[name], function(newVal, oldVal) { |
7 |
| - if (selector === true || scope.$index % 2 === selector) { |
8 |
| - 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)) |
| 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)) |
14 | 16 | newVal = map(newVal, function(v, k) { if (v) return k });
|
15 |
| - if (newVal) element.addClass(isArray(newVal) ? newVal.join(' ') : newVal); } |
16 |
| - }, true); |
17 |
| - }); |
| 17 | + if (newVal) element.addClass(isArray(newVal) ? newVal.join(' ') : newVal); |
| 18 | + } |
| 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 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + }; |
| 33 | + }]; |
18 | 34 | }
|
19 | 35 |
|
20 | 36 | /**
|
|
0 commit comments