0

I am building up a single page website using MEAN, I've tried multiple tutorials and googled a lot of examples but i cant seem to get it working. i am using angular-routing for my templates, but when I add a controller - the templates is not showing. Not any error.

app.js

angular.module('sampleApp', ['frontPageCtrl','ngRoute', 'appRoutes']);

appRoutes.js

angular.module('appRoutes', [])
.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

    $routeProvider 

        .when('/', {
            templateUrl: 'views/home.html',
            controller: 'frontController'
        })
        .otherwise({
            redirectTo: '/'
        });


    $locationProvider.html5Mode(true);
    }]);

frontPageController.js

angular.module('frontPageCtrl', [])
.controller('frontController', function($scope, $http) {
});

In my index.html file I have included all three files with app.js first and after angular include.

EDIT:

This is my index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<base href="/">

  <link rel="stylesheet" href="libs/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="libs/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="css/frontPage.css">
<link rel="stylesheet" href="libs/bootstrap/"

<!-- JS -->
<script src="libs/angular/angular.min.js"></script>
<script src="libs/angular-route/angular-route.min.js"></script>

<script src="js/app.js"></script>
<script src="js/appRoutes.js"></script>
<script src="js/controllers/frontPageController.js"></script>
</head>
<body ng-app="sampleApp" ng-controller="frontController">
<div id="container">

<div id="content">
    <div ng-view></div>

</div>
</div>
</body>
</html>
Sunil Lama
  • 4,444
  • 1
  • 14
  • 45

2 Answers2

2

Things to check

  1. Have you added ng-view in ur main landing page
  2. Have you addedd ng-app="sampleApp" in main landing page.
  3. Put break point in controller just to make sure it gets in there
  4. Check network tab and make sure all javascript files are loading

And don't forget to mark it as an answer if this answers ur question :)

Atul Chaudhary
  • 3,470
  • 26
  • 38
1

As far as i can see after viewing your code, your dependency injection is wrong. See the changes in appRoutes.js, app.js ,index.html and you need home.html with frontController inside your views folder.

Your app.js should be:

angular.module('appRoutes', []);

Your appRoutes.js should be:

    angular.module('sampleApp', ['frontPageCtrl','ngRoute', 'sampleApp'])
    .config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

        $routeProvider 

            .when('/', {
                templateUrl: 'views/home.html',
                controller: 'frontController'
            })
            .otherwise({
                redirectTo: '/'
            });


        }]);

And Your frontPageController.js:

    angular.module('frontPageCtrl', [])
    .controller('frontController', function($scope, $http) {
    });

And, In your Index.html:

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <base href="/">
      <link rel="stylesheet" href="libs/bootstrap/dist/css/bootstrap.min.css">
      <link rel="stylesheet" href="libs/font-awesome/css/font-awesome.min.css">
      <link rel="stylesheet" href="css/frontPage.css">
      <link rel="stylesheet" href="libs/bootstrap/"
      <!-- JS -->
      <script src="libs/angular/angular.min.js"></script>
      <script src="libs/angular-route/angular-route.min.js"></script>
      <script src="js/appRoutes.js"></script>
      <script src="js/app.js"></script>
      <script src="js/controllers/frontPageController.js"></script>
   </head>
   <body ng-app="sampleApp">
      <div id="container">
         <div id="content">
            <div ng-view></div>
         </div>
      </div>
   </body>
</html>

And, Your home.html should have the controller frontController. By default, your home.html will take the frontController, if ng-controller="frontController" not used.

<div ng-controller="frontController">

Here is the working plunker: http://plnkr.co/edit/ohe4U0Am83U5Hj05HOIv?p=preview

Sunil Lama
  • 4,444
  • 1
  • 14
  • 45
  • Tried this and still nothing – user2798413 Feb 23 '16 at 10:58
  • let me edit it, you were missing your index, thats why i assumed and posted it. – Sunil Lama Feb 23 '16 at 11:00
  • still not working, i even tried putting all my angular code in the same file as shershen commented on original post. – user2798413 Feb 23 '16 at 11:14
  • check out the plunker , its working, plunker link is at the bottom of my answer. If you checked your console. $location in HTML5 mode requires a tag to be present. – Sunil Lama Feb 23 '16 at 11:25
  • Where did you put the tag? dont see it in the plunker – user2798413 Feb 23 '16 at 11:56
  • Before using tag, you have to learn about the base tag, its advantages and disadvantages.The topic of understanding tag is a question itself. http://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag refer it to understand. – Sunil Lama Feb 23 '16 at 16:53