Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading Modules in Angular
    primarykey
    data
    text
    <p>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:</p> <pre><code>/app index.html login.html home.html javascript app.js loginCtrl.js homeCtrl.js </code></pre> <p>my app.js file has the following:</p> <pre><code>'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'}); }); </code></pre> <p>my loginCtrl.js file is very basic at the moment. It only has:</p> <pre><code>'use strict'; app.controller('loginCtrl', function loginCtrl($scope) { } ); </code></pre> <p>My homeCtrl.js is almost the same, except for the name. It looks like the following:</p> <pre><code>'use strict'; app.controller('homeCtrl', function homeCtrl($scope) { } ); </code></pre> <p>My index.html file is the angularSeed <a href="https://github.com/angular/angular-seed/blob/master/app/index-async.html" rel="nofollow">index-async.html</a> file. However, when I load the dependencies, I have the following:</p> <pre><code>// 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']); }); </code></pre> <p>My problem is, sometimes my app works and sometimes it doesn't. It's almost like something gets loaded before something else.</p> <p>Occasionally, I receive this <a href="http://docs.angularjs.org/error/%24injector%3amodulerr?p0=site&amp;p1=Error%3a%20%5B%24injector%3amodulerr%5D%20http://errors.angularjs.org/undefined/%24injector/modulerr?p0=ngRoute&amp;p1=Error%253A%2520No%2520module%253A%2520ngRoute%250A%2520%2520%2520%2520at%2520Error%2520%28%253Canonymous%253E%29%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A94%252Ftesting%252Fexample%252F%253A18%253A308%250A%2520%2520%2520%2520at%2520d%2520%28http%253A%252F%252Flocalhost%253A94%252Ftesting%252Fexample%252F%253A18%253A72%29%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A94%252Ftesting%252Fexample%252F%253A18%253A203%250A%2520%2520%2520%2520at%2520http%253A%252F%252Fajax.googleapis.com%252Fajax%252Flibs%252Fangularjs%252F1.2.0-rc.3%252Fangular.min.js%253A28%253A109%250A%2520%2520%2520%2520at%2520Array.forEach%2520%28native%29%250A%2520%2520%2520%2520at%2520p%2520%28http%253A%252F%252Fajax.googleapis.com%252Fajax%252Flibs%252Fangularjs%252F1.2.0-rc.3%252Fangular.min.js%253A7%253A261%29%250A%2520%2520%2520%2520at%2520e%2520%28http%253A%252F%252Fajax.googleapis.com%252Fajax%252Flibs%252Fangularjs%252F1.2.0-rc.3%252Fangular.min.js%253A28%253A48%29%250A%2520%2520%2520%2520at%2520http%253A%252F%252Fajax.googleapis.com%252Fajax%252Flibs%252Fangularjs%252F1.2.0-rc.3%252Fangular.min.js%253A28%253A126%250A%2520%2520%2520%2520at%2520Array.forEach%2520%28native%29%0A%20%20%20%20at%20Error%20%28%3Canonymous%3E%29%0A%20%20%20%20at%20http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.min.js:6:453%0A%20%20%20%20at%20http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.min.js:28:449%0A%20%20%20%20at%20Array.forEach%20%28native%29%0A%20%20%20%20at%20p%20%28http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.min.js:7:261%29%0A%20%20%20%20at%20e%20%28http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.min.js:28:48%29%0A%20%20%20%20at%20http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.min.js:28:126%0A%20%20%20%20at%20Array.forEach%20%28native%29%0A%20%20%20%20at%20p%20%28http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.min.js:7:261%29%0A%20%20%20%20at%20e%20%28http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.min.js:28:48" rel="nofollow">error</a>. 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.</p> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload