Loading Modules in Angular
Posted
by
SL Dev
on Stack Overflow
See other posts from Stack Overflow
or by SL Dev
Published on 2013-10-19T10:03:44Z
Indexed on
2013/10/19
15:54 UTC
Read the original article
Hit count: 251
angularjs
|angularjs-controller
I'm new to AngularJS. In my efforts to learn, I've relied on the AngularJS tutorials. Now, I'm trying to build an app using the AngularSeed project template. I'm trying to make my project as modular as possible. For that reason, my controllers are broken out into several files. Currently, I have the following:
/app
index.html
login.html
home.html
javascript
app.js
loginCtrl.js
homeCtrl.js
my app.js file has the following:
'use strict';
var app = angular.module('site', ['ngRoute']);
app.config(function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider.when('/login', {templateUrl: 'app/login.html', controller:'loginCtrl'});
$routeProvider.when('/home', {templateUrl: 'app/home.html', controller:'homeCtrl'});
$routeProvider.otherwise({redirectTo: '/login'});
});
my loginCtrl.js file is very basic at the moment. It only has:
'use strict';
app.controller('loginCtrl',
function loginCtrl($scope) {
}
);
My homeCtrl.js is almost the same, except for the name. It looks like the following:
'use strict';
app.controller('homeCtrl',
function homeCtrl($scope) {
}
);
My index.html file is the angularSeed index-async.html file. However, when I load the dependencies, I have the following:
// load all of the dependencies asynchronously.
$script([
'http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.min.js',
'http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular-route.min.js',
'javascript/app.js',
'javascript/loginCtrl.js',
'javascript/homeCtrl.js'
], function() {
// when all is done, execute bootstrap angular application
angular.bootstrap(document, ['site']);
});
My problem is, sometimes my app works and sometimes it doesn't. It's almost like something gets loaded before something else.
Occasionally, I receive this error. Other times, I get an error in the console window that says: 'Uncaught ReferenceError: app is not defined' in loginCtrl.js. Sometimes it happens with homeCtrl.js.
What am I doing wrong? It feels like I need to have my controllers in a module and pass that module in my app.config in the app.js file. However, a) I'm not sure if that allowed and b) I'm not sure how to do it.
© Stack Overflow or respective owner