X Tutup
Skip to content

Commit 2957b0b

Browse files
committed
fix(ng_class): support sets correctly
Previously, NgClass threw in Dart checked mode. Closes #4910
1 parent 28db864 commit 2957b0b

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

modules/angular2/src/core/directives/ng_class.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {isPresent, isString, StringWrapper, isBlank} from 'angular2/src/core/facade/lang';
1+
import {isPresent, isString, StringWrapper, isBlank, isArray} from 'angular2/src/core/facade/lang';
22
import {DoCheck, OnDestroy} from 'angular2/lifecycle_hooks';
33
import {Directive} from 'angular2/src/core/metadata';
44
import {ElementRef} from 'angular2/src/core/linker';
@@ -146,10 +146,13 @@ export class NgClass implements DoCheck, OnDestroy {
146146
this._initialClasses.forEach(className => this._toggleClass(className, !isCleanup));
147147
}
148148

149-
private _applyClasses(rawClassVal: string[] | {[key: string]: string}, isCleanup: boolean) {
149+
private _applyClasses(rawClassVal: string[] | Set<string>| {[key: string]: string},
150+
isCleanup: boolean) {
150151
if (isPresent(rawClassVal)) {
151-
if (isListLikeIterable(rawClassVal)) {
152+
if (isArray(rawClassVal)) {
152153
(<string[]>rawClassVal).forEach(className => this._toggleClass(className, !isCleanup));
154+
} else if (rawClassVal instanceof Set) {
155+
(<Set<string>>rawClassVal).forEach(className => this._toggleClass(className, !isCleanup));
153156
} else {
154157
StringMapWrapper.forEach(<{[k: string]: string}>rawClassVal, (expVal, className) => {
155158
if (expVal) this._toggleClass(className, !isCleanup);

modules/angular2/test/core/directives/ng_class_spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
it,
1515
xit,
1616
} from 'angular2/testing_internal';
17-
import {ListWrapper, StringMapWrapper} from 'angular2/src/core/facade/collection';
17+
import {ListWrapper, StringMapWrapper, SetWrapper} from 'angular2/src/core/facade/collection';
1818
import {Component, View, NgFor, provide} from 'angular2/angular2';
1919
import {NgClass} from 'angular2/src/core/directives/ng_class';
2020
import {APP_VIEW_POOL_CAPACITY} from 'angular2/src/core/linker/view_pool';
@@ -253,6 +253,29 @@ export function main() {
253253
}));
254254
});
255255

256+
describe('expressions evaluating to sets', () => {
257+
258+
it('should add and remove classes if the set instance changed',
259+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
260+
var template = '<div [ng-class]="setExpr"></div>';
261+
262+
tcb.overrideTemplate(TestComponent, template)
263+
.createAsync(TestComponent)
264+
.then((rootTC) => {
265+
var setExpr = new Set<string>();
266+
setExpr.add('bar');
267+
rootTC.debugElement.componentInstance.setExpr = setExpr;
268+
detectChangesAndCheck(rootTC, 'bar');
269+
270+
setExpr = new Set<string>();
271+
setExpr.add('baz');
272+
rootTC.debugElement.componentInstance.setExpr = setExpr;
273+
detectChangesAndCheck(rootTC, 'baz');
274+
275+
async.done();
276+
});
277+
}));
278+
});
256279
describe('expressions evaluating to string', () => {
257280

258281
it('should add classes specified in a string literal',
@@ -452,6 +475,9 @@ class TestComponent {
452475
condition: boolean = true;
453476
items: any[];
454477
arrExpr: string[] = ['foo'];
478+
setExpr: Set<string> = new Set<string>();
455479
objExpr = {'foo': true, 'bar': false};
456480
strExpr = 'foo';
481+
482+
constructor() { this.setExpr.add('foo'); }
457483
}

0 commit comments

Comments
 (0)
X Tutup