0

I need to use two different styles for two different looks of a page and I need different CSS and JavaScript files for that. Is there anyway I could conditionally render CSS and JavaScript files inside the head element in an html file?

I am working on an AngularJS application with Angular UI-Router module.

<html lang="en" ng-app="myApp">
  <head>

    <!-- if {{$state.current.name}} is "front" then list these CSS files -->
    <!-- if {{$state.current.name}} is "admin" then list these CSS files -->

  </head>
  <body>
    <!-- ... -->
    <!-- if {{$state.current.name}} is "front" then list these JavaScript files -->
    <!-- if {{$state.current.name}} is "admin" then list these JavaScript files -->
  </body>
</html>

Could somebody help me with it?

2
  • 1
    Are ng-if and ng-src of any help? As in, <link ng-if="condition" rel="stylesheet" ng-src="style.css"> and similar for scripts? Just an idea. Commented Sep 14, 2014 at 18:29
  • @idrarig: Could I not have a list of <link> elements based on a condition rather than having ng-if on every <link> element? Could I have two <head> elements and render only one of them based on <head ng-if="condition">.. </head>? Would it be valid html? Commented Sep 14, 2014 at 18:43

1 Answer 1

1

I played around a bit and came up with this gist. From index.html you can see that what you want can be achieved using the ngIf, ngSrc, and ngHref directives as I suggested in my comment. From index-grouped.html you can see that you can indeed group <link> and <script> tags that need to appear under the same condition, but I'm not sure whether my approach works cross-browser. I only tried Google Chrome.

Anyway, having two <head> elements is not valid HTML. You can verify this yourself using an online validator.

I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.