mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-18 23:50:23 +00:00
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
This commit is contained in:
parent
d3b32a7c94
commit
d859dcecea
1 changed files with 43 additions and 13 deletions
|
|
@ -3,25 +3,55 @@
|
|||
function classDirective(name, selector) {
|
||||
name = 'ngClass' + name;
|
||||
return ngDirective(function(scope, element, attr) {
|
||||
// Reusable function for re-applying the ngClass
|
||||
function ngClassWatchAction(newVal, oldVal) {
|
||||
if (selector === true || scope.$index % 2 === selector) {
|
||||
if (oldVal && (newVal !== oldVal)) {
|
||||
if (isObject(oldVal) && !isArray(oldVal))
|
||||
oldVal = map(oldVal, function(v, k) { if (v) return k });
|
||||
element.removeClass(isArray(oldVal) ? oldVal.join(' ') : oldVal);
|
||||
}
|
||||
if (isObject(newVal) && !isArray(newVal))
|
||||
newVal = map(newVal, function(v, k) { if (v) return k });
|
||||
if (newVal) element.addClass(isArray(newVal) ? newVal.join(' ') : newVal);
|
||||
}
|
||||
};
|
||||
|
||||
scope.$watch(attr[name], ngClassWatchAction, true);
|
||||
|
||||
attr.$observe('class', function(value) {
|
||||
var ngClass = scope.$eval(attr[name]);
|
||||
ngClassWatchAction(ngClass, ngClass);
|
||||
});
|
||||
|
||||
|
||||
if (name !== 'ngClass') {
|
||||
scope.$watch('$index', function($index, old$index) {
|
||||
var mod = $index % 2;
|
||||
if (mod !== old$index % 2) {
|
||||
if (mod == selector) {
|
||||
addClass(scope.$eval(attr[name]));
|
||||
} else {
|
||||
removeClass(scope.$eval(attr[name]));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function ngClassWatchAction(newVal, oldVal) {
|
||||
if (selector === true || scope.$index % 2 === selector) {
|
||||
if (oldVal && (newVal !== oldVal)) {
|
||||
removeClass(oldVal);
|
||||
}
|
||||
addClass(newVal);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function removeClass(classVal) {
|
||||
if (isObject(classVal) && !isArray(classVal)) {
|
||||
classVal = map(classVal, function(v, k) { if (v) return k });
|
||||
}
|
||||
element.removeClass(isArray(classVal) ? classVal.join(' ') : classVal);
|
||||
}
|
||||
|
||||
|
||||
function addClass(classVal) {
|
||||
if (isObject(classVal) && !isArray(classVal)) {
|
||||
classVal = map(classVal, function(v, k) { if (v) return k });
|
||||
}
|
||||
if (classVal) {
|
||||
element.addClass(isArray(classVal) ? classVal.join(' ') : classVal);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue