You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Need a way for a component to add CSS classes to host non-destructively #7289
Doing this in the component will completely overwrite the class property, wiping away the user's classes.
In other situations, NgClass serves this purpose, but a component cannot apply a directive to its host element. Using the [class.whatever] syntax also doesn't solve this, as the whatever is the variable part.
Proposal
Each component can, conceptually, have two distinct class lists: one that is from the user and one that comes from the component itself. These could be resolved independently with Angular rendering the union. Any time the user uses ngClass or the [class.whatever] syntax, it should override whatever is set to "class".
Reacted by Aleš, Paul Gschwendtner, Craig Goldstone, Bruno Gomes, tmair, Daniel Meyer, Rodrigo Quesada, Alex Jover, Laurensvdmaas, mmartini-del and 47 more
This will capture any previous classes that have been manually assigned to the components dom. However does not facilitate for ones set dynamically by e.g. directives.
Reacted by David Enke, Cristian Urbano, Lucas Basquerotto, Guilherme Ventura, Andrei, José Toledo Navarro, Douglas Eggleton, Zdeněk Haták, Nicu, Drew Coleman and 6 more
Reacted by Tobias Witt, Евгений Мирошниченко, Joel Hernández, Zama Khan Mohammed and Wenzhao Hu
Reacted by Viet Ta, Maxime Lafarie, Ravi Teja Gudapati and Nigel Tiany
Reacted by Eric Smekens, Andrianov Alexey, José Toledo Navarro, Noorsil Karedia, Wenzhao Hu, Azarus, Egor Komarov, erhise, Giampiero, Zama Khan Mohammed and 12 more
Reacted by Patricio Ezequiel Hondagneu Roig and Stalky
Activity
wembernard commentedon Mar 9, 2016
👍
JustasKuizinas commentedon Feb 27, 2017
Any updates on this?
Swiftwork commentedon Mar 30, 2017
Isn't this quite easily solved by:
This will capture any previous classes that have been manually assigned to the components dom. However does not facilitate for ones set dynamically by e.g. directives.
tsu1980 commentedon Sep 20, 2017
My workaround.
xmeng1 commentedon Oct 18, 2017
Currently, I use 3 methods to add the class name for a component. I don't know whether they are good way to do it, but it works according to my test.
@component({
host: {
'[class]': 'm-grid__item' +
' m-grid__item--fluid''},
})
@HostBinding('class.m-grid__item') public bindStyle: boolean = true;
@HostBinding('class.m-grid__item--fluid') public bindStyle2: boolean = true;
@component({
selector: 'app-test .m-grid__item.m-grid__item--fluid',
})
MartinMa commentedon Nov 8, 2017
@xmeng1 Thanks for your summary. Just a small correction for approach 1 to avoid confusion (for anyone who might read this).
It has to be
'[class]':(instead of['class']:)! The quotes have to be on the outside.xmeng1 commentedon Nov 8, 2017
@MartinMa you are right, thanks!
trotyl commentedon Dec 16, 2017
I used a manually provided
NgClassto achieve this like:More details can be found in the StackOverflow answer.
10 remaining items
greggbjensen commentedon Jul 9, 2019
This worked well for me and was a simple solution:
jpcmf commentedon Aug 21, 2019
All examples
I've just add
@HostBinding('class') classList = 'd-flex flex-column h-100';to myapp.componentand it works. Thank you @faizalmy 😃fix: a bug with overriding component class via HostBinding
etay2000 commentedon Oct 8, 2019
Wow another common use case issue lingering since 2016. Perhaps my open source software expectations are too high?
trotyl commentedon Oct 9, 2019
@etay2000 It has already been fixed in Ivy for some time, just wan't brought back to view engine.
etay2000 commentedon Oct 9, 2019
Ok thanks, I appreciate the info.
exequiel09 commentedon Dec 20, 2019
I'm having an issue with
HostBindingin Angular v9.0.0-rc.6 which outputs the string[MAP].I am currently using @faizalmy reply here in this issue
/cc @kara @jelbourn
vthinkxie commentedon Jan 8, 2020
this issue has been fixed in angular 9.0.0
anyone who meet this issue can try the below codes in angular 9
PeterHewat commentedon Jan 27, 2020
I confirm this works in Angular 9.0.0
Here is a simple example:
Then, if you add a class to that component as follows...
... both myRedBorder and myYellowBackground classes will be applied to the instance of
<app-root/>wnvko commentedon Feb 5, 2020
I think this is some easy way for adding class to component:
And then use component like this:
@vladimiry you can add ass much classes as you need like this:
and then several inline classes too:
vladimiry commentedon Feb 5, 2020
@wnvko adding one class is easy, the issue is about host-bind a multiple classes.
mhevery commentedon Feb 6, 2020
This has been resolved as of #34938 Closing the issue.
angular-automatic-lock-bot commentedon Mar 8, 2020
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.