Search code examples
htmlcssangularjsngrouteng-view

CSS not loaded in other views when routing in angularjs


Hey so I'm routing between views in Angularjs using routeProvider and ng-view. My issue is that my css does not get loaded when the route changes. I tried including Angular-css so that the css could be included in each view but it doesn't seem to work. It gets loaded in the first page loaded from ng-view (home.html), but when I link to another page from there the css stops getting loaded.

index.html:

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

    <script src="app/lib/jquery-3.0.0.js"></script>
    <link href="styles.css" rel="stylesheet" type="text/css" />

    <script src="app/lib/angular.min.js"></script>
    <script src="app/lib/angular-route.min.js"></script>
    <script src="app/lib/angular-css.min.js"></script>
    <script src="app/lib/app.js"></script>
  </head>
  <body>

    <main ng-view>
    </main>

  </body>

app.js

var myApp = angular.module('myApp', ['ngRoute', 'angularCSS']);
myApp.config(['$routeProvider', function($routeProvider){
$routeProvider
  .when('/login', {
    templateUrl: 'views/login.html',
    css: 'styles.css'
  })
  .when('/home', {
    templateUrl: 'views/home.html',
    css: 'styles.css'
  })
  .when('/signup', {
    templateUrl: 'views/signup.html',
    css: 'styles.css'
  })
  .when('/submitdish', {
    templateUrl: 'views/submitdish.html',
    css: 'styles.css'
  })
  .when('/login', {
    templateUrl: 'views/login.html',
    css: 'styles.css'
  })
  .otherwise({
    redirectTo: '/home',
    css: 'styles.css'
  });
}]);

home.html:

<ul class="nav">
     <a align="center" href="views/home.html"><li>Home</li></a>
     <a align="center" href=""><li>About</li></a>
     <a align="center" href="#"><li>Contact</li></a>
  </ul>

<section class="container">
  <div class="left-half">
    <article>
      <a href="views/submitdish.html" class="Button">Sell a Dish</a>

      <p>Something Something Something about Selling your home-cooked dishes</p>
    </article>
  </div>
  <div class="right-half">
    <article>
      <a href="views/buydish.html" class="Button">Buy a Dish</a>
      <p>Yada yada yada buy food from your neighbors</p>
    </article>
  </div>
</section>

Solution

  • Ok I figured it out the problem was I was linking to the other views using

    <a href="views/submitdish.html" class="Button">Sell a Dish</a>

    when I should have been linking using #/submitdish (so as to properly use the routeProvider)

    <a href="#/submitdish" class="Button">Sell a Dish</a>