Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit a9742f9

Browse files
committed
fixup! refactor(ngClass): simplify conditions
1 parent f766698 commit a9742f9

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/ng/directive/ngClass.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
function classDirective(name, selector) {
1010
name = 'ngClass' + name;
11+
var indexWatchExpression;
1112

1213
return ['$parse', function($parse) {
1314
return {
@@ -32,7 +33,14 @@ function classDirective(name, selector) {
3233
}
3334

3435
if (name !== 'ngClass') {
35-
scope.$watch('$index', ngClassIndexWatchAction);
36+
if (!indexWatchExpression) {
37+
indexWatchExpression = $parse('$index', function moduloTwo($index) {
38+
// eslint-disable-next-line no-bitwise
39+
return $index & 1;
40+
});
41+
}
42+
43+
scope.$watch(indexWatchExpression, ngClassIndexWatchAction);
3644
}
3745

3846
scope.$watch(watchExpression, watchAction, isOneTime);
@@ -76,22 +84,17 @@ function classDirective(name, selector) {
7684
return classesToUpdate.join(' ');
7785
}
7886

79-
function ngClassIndexWatchAction($index) {
80-
// eslint-disable-next-line no-bitwise
81-
var newModulo = $index & 1;
82-
83-
if (newModulo !== oldModulo) {
84-
// This watch-action should run before the `ngClass[OneTime]WatchAction()`, thus it
85-
// adds/removes `oldClassString`. If the `ngClass` expression has changed as well, the
86-
// `ngClass[OneTime]WatchAction()` will update the classes.
87-
if (newModulo === selector) {
88-
addClasses(oldClassString);
89-
} else {
90-
removeClasses(oldClassString);
91-
}
92-
93-
oldModulo = newModulo;
87+
function ngClassIndexWatchAction(newModulo) {
88+
// This watch-action should run before the `ngClass[OneTime]WatchAction()`, thus it
89+
// adds/removes `oldClassString`. If the `ngClass` expression has changed as well, the
90+
// `ngClass[OneTime]WatchAction()` will update the classes.
91+
if (newModulo === selector) {
92+
addClasses(oldClassString);
93+
} else {
94+
removeClasses(oldClassString);
9495
}
96+
97+
oldModulo = newModulo;
9598
}
9699

97100
function ngClassOneTimeWatchAction(newClassValue) {

0 commit comments

Comments
 (0)