@@ -96,12 +96,15 @@ function classDirective(name, selector) {
96
96
}
97
97
98
98
function arrayClasses ( classVal ) {
99
+ var classes = [ ] ;
99
100
if ( isArray ( classVal ) ) {
100
- return classVal . join ( ' ' ) . split ( ' ' ) ;
101
+ forEach ( classVal , function ( v ) {
102
+ classes = classes . concat ( arrayClasses ( v ) ) ;
103
+ } ) ;
104
+ return classes ;
101
105
} else if ( isString ( classVal ) ) {
102
106
return classVal . split ( ' ' ) ;
103
107
} else if ( isObject ( classVal ) ) {
104
- var classes = [ ] ;
105
108
forEach ( classVal , function ( v , k ) {
106
109
if ( v ) {
107
110
classes = classes . concat ( k . split ( ' ' ) ) ;
@@ -129,16 +132,18 @@ function classDirective(name, selector) {
129
132
* 1. If the expression evaluates to a string, the string should be one or more space-delimited class
130
133
* names.
131
134
*
132
- * 2. If the expression evaluates to an array, each element of the array should be a string that is
133
- * one or more space-delimited class names.
134
- *
135
- * 3. If the expression evaluates to an object, then for each key-value pair of the
135
+ * 2. If the expression evaluates to an object, then for each key-value pair of the
136
136
* object with a truthy value the corresponding key is used as a class name.
137
137
*
138
+ * 3. If the expression evaluates to an array, each element of the array should either be a string as in
139
+ * type 1 or an object as in type 2. This means that you can mix strings and objects together in an array
140
+ * to give you more control over what CSS classes appear. See the code below for an example of this.
141
+ *
142
+ *
138
143
* The directive won't add duplicate classes if a particular class was already set.
139
144
*
140
- * When the expression changes, the previously added classes are removed and only then the
141
- * new classes are added.
145
+ * When the expression changes, the previously added classes are removed and only then are the
146
+ * new classes added.
142
147
*
143
148
* @animations
144
149
* **add** - happens just before the class is applied to the elements
@@ -167,17 +172,24 @@ function classDirective(name, selector) {
167
172
<input ng-model="style1" placeholder="Type: bold, strike or red"><br>
168
173
<input ng-model="style2" placeholder="Type: bold, strike or red"><br>
169
174
<input ng-model="style3" placeholder="Type: bold, strike or red"><br>
175
+ <hr>
176
+ <p ng-class="[style4, {orange: warning}]">Using Array and Map Syntax</p>
177
+ <input ng-model="style4" placeholder="Type: bold, strike"><br>
178
+ <input type="checkbox" ng-model="warning"> warning (apply "orange" class)
170
179
</file>
171
180
<file name="style.css">
172
181
.strike {
173
- text-decoration: line-through;
182
+ text-decoration: line-through;
174
183
}
175
184
.bold {
176
185
font-weight: bold;
177
186
}
178
187
.red {
179
188
color: red;
180
189
}
190
+ .orange {
191
+ color: orange;
192
+ }
181
193
</file>
182
194
<file name="protractor.js" type="protractor">
183
195
var ps = element.all(by.css('p'));
@@ -202,11 +214,18 @@ function classDirective(name, selector) {
202
214
});
203
215
204
216
it('array example should have 3 classes', function() {
205
- expect(ps.last( ).getAttribute('class')).toBe('');
217
+ expect(ps.get(2 ).getAttribute('class')).toBe('');
206
218
element(by.model('style1')).sendKeys('bold');
207
219
element(by.model('style2')).sendKeys('strike');
208
220
element(by.model('style3')).sendKeys('red');
209
- expect(ps.last().getAttribute('class')).toBe('bold strike red');
221
+ expect(ps.get(2).getAttribute('class')).toBe('bold strike red');
222
+ });
223
+
224
+ it('array with map example should have 2 classes', function() {
225
+ expect(ps.last().getAttribute('class')).toBe('');
226
+ element(by.model('style4')).sendKeys('bold');
227
+ element(by.model('warning')).click();
228
+ expect(ps.last().getAttribute('class')).toBe('bold orange');
210
229
});
211
230
</file>
212
231
</example>
0 commit comments