[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) {
    ...
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s