I have a <myComponent ></myComponent> and I want give class to myComponent?
This doesnt worked `<myComponent class="my-class"></myComponent>`
How can I do that?
I have a <myComponent ></myComponent> and I want give class to myComponent?
This doesnt worked `<myComponent class="my-class"></myComponent>`
How can I do that?
class MyComponent {
@HostBinding('class.my-class')
isMyClass:boolean = true;
}
isMyClass is just a name I chose. The name doesn't matter. 'class.my-class' defines what class should be added or removed. The value of the property where @HostBinding() is prepended is used to determine if the class should be added or removed (true/false). The only thin where the name of isMyClass is relevent, is when you want to change the value like this.isMyClass = false;my-class to the host element of the MyComponent component. You can now style it from the parent elements styleUrls: ['parent.css'] using :host my-component.my-class { ... } or from MyComponents styleUrls: [my-component.css'] using :host-context(.my-class) { ... }Don't think that will work but there are easy workarounds:
If you want to style the placement/color etc of your component:
Best workaround is to place your <myComponent></myComponent> inside a <div> and apply the class to that div
If you want to style the contents of your component:
adding styleUrls in your Component and styling your content with a css file
You can use @Input for your component and set external class in any place.
Example:
<myComponent [styleClass]="'my-class'"></myComponent>>
Your component
/my-component.component.ts
...
@Input() styleClass: string;
...
/my-component.component.html
<div [class]="styleClass" ngClass="my-component__container any-other-class">
Component works!
</div>