Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions modules/angular2/src/directives/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ import {ListWrapper, StringMapWrapper, isListLikeIterable} from 'angular2/src/fa
* </div>
* ```
*/
@Directive(
{selector: '[class]', lifecycle: [LifecycleEvent.onCheck], properties: ['rawClass: class']})
@Directive({
selector: '[class]',
lifecycle: [LifecycleEvent.onCheck, LifecycleEvent.onDestroy],
properties: ['rawClass: class']
})
export class CSSClass {
_pipe: Pipe;
_rawClass;
Expand Down Expand Up @@ -58,6 +61,8 @@ export class CSSClass {
}
}

onDestroy(): void { this._cleanupClasses(this._rawClass); }

private _cleanupClasses(rawClassVal): void {
if (isPresent(rawClassVal)) {
if (isListLikeIterable(rawClassVal)) {
Expand Down
28 changes: 26 additions & 2 deletions modules/angular2/test/directives/class_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,36 @@ import {
xit,
} from 'angular2/test_lib';
import {List, ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
import {Component, View} from 'angular2/angular2';
import {Component, View, NgFor, bind} from 'angular2/angular2';
import {DOM} from 'angular2/src/dom/dom_adapter';
import {CSSClass} from 'angular2/src/directives/class';
import {APP_VIEW_POOL_CAPACITY} from 'angular2/src/core/compiler/view_pool';

export function main() {
describe('binding to CSS class list', () => {

describe('viewpool support', () => {
beforeEachBindings(() => { return [bind(APP_VIEW_POOL_CAPACITY).toValue(100)]; });

it('should clean up when the directive is destroyed',
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var template = '<div *ng-for="var item of items" [class]="item"></div>';
tcb.overrideTemplate(TestComponent, template)
.createAsync(TestComponent)
.then((rootTC) => {
rootTC.componentInstance.items = [['0']];
rootTC.detectChanges();
rootTC.componentInstance.items = [['1']];
rootTC.detectChanges();
expect(rootTC.componentViewChildren[1].nativeElement.className)
.toEqual('ng-binding 1');

async.done();
});
}));
});


describe('expressions evaluating to objects', () => {

it('should add classes specified in an object literal',
Expand Down Expand Up @@ -344,9 +367,10 @@ export function main() {
}

@Component({selector: 'test-cmp'})
@View({directives: [CSSClass]})
@View({directives: [CSSClass, NgFor]})
class TestComponent {
condition: boolean = true;
items: any[];
arrExpr: List<string> = ['foo'];
objExpr = {'foo': true, 'bar': false};
strExpr = 'foo';
Expand Down