This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +28
-1
lines changed
2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -527,8 +527,12 @@ function ngClass(selector) {
527
527
scope . $watch ( expression , function ( newVal , oldVal ) {
528
528
if ( selector ( scope . $index ) ) {
529
529
if ( oldVal && ( newVal !== oldVal ) ) {
530
+ if ( isObject ( oldVal ) && ! isArray ( oldVal ) )
531
+ oldVal = map ( oldVal , function ( v , k ) { if ( v ) return k } ) ;
530
532
element . removeClass ( isArray ( oldVal ) ? oldVal . join ( ' ' ) : oldVal ) ;
531
533
}
534
+ if ( isObject ( newVal ) && ! isArray ( newVal ) )
535
+ newVal = map ( newVal , function ( v , k ) { if ( v ) return k } ) ;
532
536
if ( newVal ) element . addClass ( isArray ( newVal ) ? newVal . join ( ' ' ) : newVal ) ;
533
537
}
534
538
} ) ;
@@ -551,7 +555,8 @@ function ngClass(selector) {
551
555
*
552
556
* @element ANY
553
557
* @param {expression } expression {@link guide/dev_guide.expressions Expression } to eval. The result
554
- * of the evaluation can be a string representing space delimited class names or an array.
558
+ * of the evaluation can be a string representing space delimited class
559
+ * names, an array, or a map of class names to boolean values.
555
560
*
556
561
* @example
557
562
<doc:example>
Original file line number Diff line number Diff line change @@ -200,6 +200,28 @@ describe("directive", function() {
200
200
} ) ) ;
201
201
202
202
203
+ it ( 'should support adding multiple classes conditionally via a map of class names to boolean' +
204
+ 'expressions' , inject ( function ( $rootScope , $compile ) {
205
+ var element = $compile (
206
+ '<div class="existing" ' +
207
+ 'ng:class="{A: conditionA, B: conditionB(), AnotB: conditionA&&!conditionB}">' +
208
+ '</div>' ) ( $rootScope ) ;
209
+ $rootScope . conditionA = true ;
210
+ $rootScope . $digest ( ) ;
211
+ expect ( element . hasClass ( 'existing' ) ) . toBeTruthy ( ) ;
212
+ expect ( element . hasClass ( 'A' ) ) . toBeTruthy ( ) ;
213
+ expect ( element . hasClass ( 'B' ) ) . toBeFalsy ( ) ;
214
+ expect ( element . hasClass ( 'AnotB' ) ) . toBeTruthy ( ) ;
215
+
216
+ $rootScope . conditionB = function ( ) { return true } ;
217
+ $rootScope . $digest ( ) ;
218
+ expect ( element . hasClass ( 'existing' ) ) . toBeTruthy ( ) ;
219
+ expect ( element . hasClass ( 'A' ) ) . toBeTruthy ( ) ;
220
+ expect ( element . hasClass ( 'B' ) ) . toBeTruthy ( ) ;
221
+ expect ( element . hasClass ( 'AnotB' ) ) . toBeFalsy ( ) ;
222
+ } ) ) ;
223
+
224
+
203
225
it ( 'should support adding multiple classes via a space delimited string' , inject ( function ( $rootScope , $compile ) {
204
226
var element = $compile ( '<div class="existing" ng:class="\'A B\'"></div>' ) ( $rootScope ) ;
205
227
$rootScope . $digest ( ) ;
You can’t perform that action at this time.
0 commit comments