I know a good practice is to keep css code in a external .css file, instead on adding tags in the html code. But what about css code that is only valid for a single angular partial html?
Thanks for your advice.
I know a good practice is to keep css code in a external .css file, instead on adding tags in the html code. But what about css code that is only valid for a single angular partial html?
Thanks for your advice.
As a best practice, CSS files must always be put in a dedicated [fileName].css
.
If you want to have some CSS applied only to some part of the code (as it always is using CSS), you specify dedicated rules.
Very easy example (2 partials, 1 stylesheet)
first:
<div id="partial1">
<h1>I'm the first</h1>
</div>
second:
<div id="partial2">
<h1>I'm the second</h1>
</div>
stylesheet:
// Common rule to both the <h1>
h1 {
font-size: 64px;
}
// only for partial1
#partial1 h1 {
color: #C0C0C0;
}
//only for partial2
#partial2 h1 {
color: #FF0000;
}
Edit:
You can also use SASS and have a different file for every partial (for example _partial1.scss
and _partial2.scss
), that gets included in your main.scss
, something like this:
@import "partial1",
"partial2";
See more about SASS here: http://sass-lang.com/guide
I highly recommend to put it on an external CSS file. If the styles that are only used for a single page are not too heavy, just put it with your main CSS file.
Remember that another file will make another request to the server.
If you still want to add another CSS file with Angular, you should do it with $rootScope :
$rootScope.$on('$routeChangeSuccess', function(ev,data) {
$rootScope.routeName = data.$$route.originalPath;
});
Then, in your layout file :
<link ng-if="routeName === 'myPartial' " href="css/myPartial.css" rel="stylesheet" type="text/css" />
Some docs about AngularJS rootScope : https://docs.angularjs.org/api/ng/service/$rootScope