1

i have the following code,

createComponent(message, some_css_class) {
    this.msg.reset();
    const factory = this.resolver.resolveComponentFactory(MessageComponent);
    const componentRef = this.entry.createComponent(factory);
    componentRef.instance.message = message;
    //how to add the passed css class to the this component
}

here i dynamically create the component, now need to add the passed css class to it.

2 Answers 2

1

In Angular 5/6, using Renderer2 from @angular/core, you can try like below,

constructor(private resolver: ComponentFactoryResolver, private renderer2: Renderer2) { }

createComponent(message, some_css_class) {
    this.msg.reset();
    const factory = this.resolver.resolveComponentFactory(MessageComponent);
    const componentRef = this.entry.createComponent(factory);
    componentRef.instance.message = message;
    this.renderer2.addClass(componentRef.location.nativeElement, some_css_class);
}
Sign up to request clarification or add additional context in comments.

Comments

0

you could use componentRef.instance.cssClass = some_css_class and @HostBinding('class') public cssClass; inside of a component

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.