@@ -7,21 +7,46 @@ function classDirective(name, selector) {
7
7
function ngClassWatchAction ( newVal , oldVal ) {
8
8
if ( selector === true || scope . $index % 2 === selector ) {
9
9
if ( oldVal && ( newVal !== oldVal ) ) {
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 ) ;
10
+ removeClass ( oldVal ) ;
13
11
}
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 ) ;
12
+ addClass ( newVal ) ;
17
13
}
18
14
} ;
19
- scope . $watch ( attr [ name ] , ngClassWatchAction , true ) ;
15
+
16
+ function removeClass ( classVal ) {
17
+ if ( isObject ( classVal ) && ! isArray ( classVal ) )
18
+ classVal = map ( classVal , function ( v , k ) { if ( v ) return k } ) ;
19
+ element . removeClass ( isArray ( classVal ) ? classVal . join ( ' ' ) : classVal ) ;
20
+ }
21
+
22
+ function addClass ( classVal ) {
23
+ if ( isObject ( classVal ) && ! isArray ( classVal ) )
24
+ classVal = map ( classVal , function ( v , k ) { if ( v ) return k } ) ;
25
+ if ( classVal ) element . addClass ( isArray ( classVal ) ? classVal . join ( ' ' ) : classVal ) ;
26
+ }
27
+
28
+ scope . $watch ( attr [ name ] , ngClassWatchAction , true ) ;
29
+
30
+ function indexWatch ( newVal , oldVal ) {
31
+ if ( newVal !== oldVal && ( ( newVal + oldVal ) % 2 === 1 ) ) {
32
+ if ( newVal % 2 !== selector ) {
33
+ removeClass ( scope . $eval ( attr [ name ] ) ) ;
34
+ } else {
35
+ addClass ( scope . $eval ( attr [ name ] ) ) ;
36
+ }
37
+ }
38
+ } ;
39
+
40
+ if ( selector !== true ) {
41
+ indexWatch ( scope . $index ) ;
42
+ scope . $watch ( "$index" , indexWatch , true ) ;
43
+ }
20
44
21
45
attr . $observe ( 'class' , function ( value ) {
22
46
var ngClass = scope . $eval ( attr [ name ] ) ;
23
47
ngClassWatchAction ( ngClass , ngClass ) ;
24
48
} ) ;
49
+
25
50
} ) ;
26
51
}
27
52
0 commit comments