Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to lazy load CSS files in Angular?

I'm using AngularJS. Up to this point, I've loaded CSS that is specific to my partials by linking it in at the top of the partial:

<!-- File: some_partial.html -->
<link rel="stylesheet" href="link/to/some_partial.css" />
<!-- Rest of partial code here -->

This doesn't feel right. The CSS isn't linked in the <head> (making it non-standard), and every once in awhile, the styles don't actually get applied to the partial.

What is the correct way to lazy load CSS in Angular, deferring until the partial is actually needed?

like image 830
Lukas Avatar asked Nov 13 '22 13:11

Lukas


1 Answers

May I suggest introducing a service that keeps track of the styling needs of your various controllers. It would indeed have its own little controller outputting the actual CSS files/paths into the head on basis of what other components request. This service would be injected into other components so that these can call methods to express what styling they're interested in. Expressing the styling they're interested in may be done in terms of the actual CSS files/paths or being abstracted away into something like "need support for tables, alerts, custom branding, etc.".

like image 158
Sander Verhagen Avatar answered Nov 15 '22 05:11

Sander Verhagen