Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am currently learning Angular 2. I understood how to use the angular renderer to set an ElementStyle, but now i would like to use the Angular renderer function

setElementClass(renderElement: any, className: string, isAdd: boolean) : void

My question is how can I import a css class to my attribute directive ? Do I have to convert my css class to Json?, Or can I somehow add an css class to my @ngModule ?

share|improve this question
    
What do you mean by importing a css class? className is string so why you have to load or convert or import css class? – jaguwalapratik 15 hours ago

Example of how to use Renderer and ElementRef to add css class to element.

@Directive({
   selector: '[whatever]'
})
class WhateverDirective {
   constructor(renderer: Renderer, el: ElementRef) {
       renderer.setElementClass(el.nativeElement, 'whatever-css-class');
   }
}

The whatever-css-class is defined in a css file, which is referenced in html

share|improve this answer
    
Yes absolutely, so far I understood the setElementClass function, but where lays the whatever-css-class, an attribute directive doesn't have an css file or is there a possibility to add one ? – Daniel Hoppe Alvarez 15 hours ago
    
Right, so what you can do is define class in css file which you are referencing in html, so that way it will be applied to the element. – jaguwalapratik 15 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.