[AngularJS] Solution for Main page not display

Background : Angularjs application (angularjs version 1.3.15)
Problem : main page display nothing

Demo code (with a small small error but serious):

In app.js

angular.module('myApp', [
  'ngCookies',
  'ngResource',
  'ngSanitize',
  'ngRoute'
])
  .config(function($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  });

In main.js

console.log('Before main controller');
var app = angular.module('myApp', []);
app.controller('MainCtrl', function ($scope) {
    console.log('In main controller');
});
console.log('After main controller');

In my console, it displays only :

Before main controller
After main controller

What’s wrong ??

Reason :
I’ve declared myApp twice, once in app.js and another in main.js.
Note: app.js is loaded before main.js.
The one in main.js with a parameter [], which overrides all the dependencies declared in app.js. That’s why the ngRoute not work and the main page displays nothing.
Find the answer in stack overflow.

Correction (remove the second parameter [] in main.js) :

var app = angular.module('myApp');

Total main.js

var app = angular.module('myApp');
app.controller('MainCtrl', function ($scope) {
    ...
});

Leave a comment