Angular 2: When to use attribute directives and when to use component styles?
There are 2 main ways to style an element in Angular 2 described in the official Angular 2 Docs.
Component styles can be implementated in these ways: (Template Inline Styles, Style URLs in Metadata, Template Link Tags, CSS @imports)
I'm interested in which ways to use to apply styles to elements, when.
Since they're all mentioned in the official docs, each of them may be best to use in certain circumstances (even template inline styles in some circumstances??)?
The docs demo attribute directives are responding to user events. They demo component styles without events.
Does this mean that in most circumstances, it is good practice to use attribute directives when styling is applied in response to events, and component styles when styling is applied without events?
Another hint from the official angular 2 style guide that I just found:
style 06-01
Do use attribute directives when you have presentation logic without a template.
The only reason I can think of to have presentation logic without a template is to make it usable in more than one component.
We could easily define a global css style with :hover
and use it in one to many templates. That is also
presentation logic without a template
The difference in usage is:
<div tohHighlight>Bombasta</div>
vs <div class="tohHighlight">Bombasta</div>
I can't really see a benefit to one over the other but the Angular 2 official docs always demo attribute directives with an event, and for any styling that doesn't involve an event, they use css.
In this example in style 06-01, it is yet again, in an event (mouseover).
So I think they believe it's best to use attribute directives when an event is involved.
I'm just learning Angular 2 so am not sure on the pros and cons of each method of styling, but here are some possible ones I can think of:
Possible benefits of attribute directives over css
Possible cons of attribute directives over css
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With