SL Dev
SL Dev

Reputation: 865

Loading Modules in Angular

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.

Upvotes: 2

Views: 3882

Answers (2)

KayakDave
KayakDave

Reputation: 24676

angular-seed has had problems with an out of date loader.js (which handles $script) (issue: https://github.com/angular/angular-seed/pull/111). And this causes exactly the problem you're seeing.

Try getting the most recent loader.js (https://github.com/angular/angular.js/blob/master/src/loader.js)

And grab prefix https://github.com/angular/angular.js/blob/master/src/loader.prefix

and suffix https://github.com/angular/angular.js/blob/master/src/loader.suffix

Or you could use the regular <script> tag approach and just make sure everything is included in the right order (as @Chandermani details)

Upvotes: 1

Chandermani
Chandermani

Reputation: 42669

Well I am not sure how $script works, but if it's job is to load the js files async, then you cannot determine the order in which the files are loaded and executed.

In your case you some scripts have to be loaded before others. So the angular* scripts need to be loaded and executed before your app scripts are loaded. I think the order should be

  1. Angular.min.js
  2. angular-route.min.js
  3. Then your script app.js
  4. Then your controller\directives\services scripts in any order.

Upvotes: 1

Related Questions