Skip to content

Commit

Permalink
fix(directive): reapply ng-class when interpolated class attribute ch…
Browse files Browse the repository at this point in the history
…anges

Closes angular#1016
  • Loading branch information
maxmart committed Jun 7, 2012
1 parent e7e438f commit 9474cc1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
4 changes: 1 addition & 3 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -999,9 +999,7 @@ function $CompileProvider($provide) {
// we define observers array only for interpolated attrs
// and ignore observers for non interpolated attrs to save some memory
attr.$$observers[name] = [];
if (name !== 'class') {
attr[name] = undefined;
}
attr[name] = undefined;
scope.$watch(interpolateFn, function(value) {
attr.$set(name, value);
});
Expand Down
44 changes: 18 additions & 26 deletions src/ng/directive/ngClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,27 @@

function classDirective(name, selector) {
name = 'ngClass' + name;
return ['$interpolate', function($interpolate) {
return function(scope, element, attr) {
// Reusable function for re-applying the ngClass
function reapply(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);
return ngDirective(function(scope, element, attr) {
// Reusable function for re-applying the ngClass
function reapply(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);
}
};
scope.$watch(attr[name], reapply, true);

// Watch class attribute for changes
if (attr['class']) {
var interpolateFn = $interpolate(attr['class'], true);
scope.$watch(interpolateFn, function(newClass, oldClass) {
// Reapply ngClass
var ngClass = scope.$eval(attr[name]);
reapply(ngClass, ngClass);
});
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], reapply, true);

attr.$observe('class', function(value) {
var ngClass = scope.$eval(attr[name]);
reapply(ngClass, ngClass);
});
});
}

/**
Expand Down

0 comments on commit 9474cc1

Please sign in to comment.