angular2 angular2 - 1 month ago 25
TypeScript Question

How to perform DOM manipulation in Angular2 components

How do we get hold of DOM elements in angular2 Version 2.0.1? The basic functions like addClass, removeClass etc are not availble in typescript so how do we do these DOM manipulations in angular2 components? Please suggest if any. TIA

Answer

The basic functions like addClass, removeClass etc are not availble in typescript

They are available. Everything that can be done in JS can be done in TypeScript. You can also use jQuery while it's discouraged to be used with Angular2

Some explanation how to get a reference to an element angular 2 / typescript : get hold of an element in the template

That being said, in Angular2 you should avoid modifying the DOM imperatively but instead use binding with structural directives like *ngFor, *ngIf, ..., binding like [class.class-name']="true" or directives like ngClass.

For more details see https://angular.io/docs/ts/latest/guide/