4

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>
n daniel
  • 148
  • 1
  • 10
Paul Bridi
  • 99
  • 1
  • 6

3 Answers3

1

Try to change in your injection

What you have

var myApp = angular.module('myApp', ['ngRoute', 'angularCSS']);

To

var myApp = angular.module('myApp', ['ngRoute', 'door3.css']);

Just if you are using this project and version 1.0.7

http://door3.github.io/angular-css

Also verify your CSS paths, you have to be explicit even if your .html and css are in the same folder

    var myApp = angular.module('myApp', ['ngRoute', 'angularCSS']);

    myApp.config(['$routeProvider', function($routeProvider){

    $routeProvider
      .when('/login', {
        templateUrl: 'views/login.html',
        css: 'views/styles.css'
      }); 
}]);

Or if is in a different folder

var myApp = angular.module('myApp', ['ngRoute', 'angularCSS']);

        myApp.config(['$routeProvider', function($routeProvider){

        $routeProvider
          .when('/login', {
            templateUrl: 'views/login.html',
            css: 'Content/styles.css'
          }); 
    }]);
Manuel Espino
  • 273
  • 3
  • 14
1

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>
Paul Bridi
  • 99
  • 1
  • 6
1

By default, $routeProvider and $locationProvider look for '#' in the URL--"hashbang mode".

For example, http://example.com/#/home would use the templateURL for home, with your original settings.

If you don't want to use the hash, set $locationProvider.html5Mode to true.The teeny example below will let you use browser URLs like http://www.example.com/base/path

Configuration:

$routeProvider
  .when('/path', {
   templateUrl: 'path.html',
});
$locationProvider
  .html5Mode(true);

HTML (note the base href in the head)

<html>
  <head>
    <base href="/">
  </head>
  <body>
    <a href="/path">link</a> 
  </body>
</html>    
n daniel
  • 148
  • 1
  • 10