2

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.

SL Dev
  • 855
  • 3
  • 11
  • 13
  • See this link, might help you, add `resolve` to provider: http://stackoverflow.com/questions/11972026/delaying-angularjs-route-change-until-model-loaded-to-prevent-flicker – Maxim Shoustin Oct 19 '13 at 10:21
  • It seems to be working for me. What server are you using ? What does the login.html look like ? – vinaut Oct 19 '13 at 14:18

2 Answers2

1

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.
Chandermani
  • 42,589
  • 12
  • 85
  • 88
  • 1
    $script is an async loader. So it ensures that all scripts are loaded before calling it's function (bootstrap in this case). $script makes it so you don't have to worry about order. – KayakDave Oct 19 '13 at 15:52
1

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)

KayakDave
  • 24,636
  • 3
  • 65
  • 68