modularity when it comes to AngularJS and CSS

68 views Asked by At

What is the convention when it comes to AngularJS directives and styling them?

Do I style via JS or stylesheet? How do I keep things self-contained and modular?

2

There are 2 answers

0
jsSn0wMan On

There is no convention as this is an issue with other libraries like extjs. However, in extjs, each component has a config to put styles in much like inline styles.

My approach is to include a separate css with the directive as it nicely separates the styling. And since angular directives allow separate template, your html is nicely separated from the javascript as well.

0
Liglo App On

It depends heavily on the framework you use. In many MVC frameworks, you can write your own view helpers which automatically append a JS-file and CSS-file belonging to the same directive. Otherwise, you can use directive templates or Shadow DOM.