0

My scripts are :

index.html

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title>My Application</title>
</head>
<body>
    <div ng-view></div>

    <script src="/assets/vendor/angular/angular-1.2.16.min.js"></script>
    <script src="/assets/vendor/angular/extras/angular-route.js"></script>
    <script src="/assets/myapp/myApp.js"></script>
</body>
</html>

myApp.js

(function () {
    angular.module('myApp', ['ngRoute'])
           .config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
               $routeProvider.
                 when('/', {
                     template: '<h1>Home</h1>',
                     controller: function () {
                         console.log('Home');
                     }
                 }).
                 when('/books', {
                     template: '<h1>Books</h1>',
                     controller: function () {
                         console.log('Books');
                     }
                 });
               $locationProvider.html5Mode(true);
               console.log('routes configured');
           }]);
})();

I've wasted a lot of time trying to figure out what the problem might be with no luck. Am I missing something silly? thanks in advance for the help.

Annsh
  • 13
  • 3
snajahi
  • 900
  • 2
  • 14
  • 26

2 Answers2

1

Are you hosting your application in the root of your server? If not, then you will need to use the tag below in your head tag.

<base href="PATH_HERE" />

In addition, can you comment out the $locationProvider.html5Mode(true) line and get your app working in hash routing mode first?

romiem
  • 8,360
  • 7
  • 29
  • 36
  • Please say more. I am experiencing this problem in an webapp whose server is reverse proxied under nginx. nginx has no idea where the app route is as that is the business of the backend webserver. Should this work? –  May 22 '14 at 23:01
  • The base tag is only used if you are hosting your angular application somewhere other than the root of your web server. For example (and I am assuming html5Mode is off), if you had a server http://myserver.com and your angular application was hosted in the root, then your angular routes would be defined at http://myserver.com/#/someroute. However, if you were hosting your angular app at http://myserver.com/subdirectory/ then you would have to set the href of the base tag to /subdirectory/ so that your routes correctly resolve to http://myserver.com/subdirectory/#/someroute – romiem May 23 '14 at 11:01
0

Just a question, but it is possible to add a function in the controller -> controller:function()? Because normally i would do it like this. controller:'mainController' and put the function in the mainController.

Mike
  • 143
  • 2
  • 12