4

By default, AngularJS doesn't seem to work unless I put it in the <head>. Is there a way to put it at the end of the <body> instead?

My code looks like this in the footer:

$(document).ready(function() {
    var myApp = angular.module("myApp", []);

    myApp.bootstrap(document, ["myApp"]);

    myApp.controller("AppController",["$scope", "$http", function() {
        // so stuff with the $scope.
    }]);
});

EDIT 3/31/14: Based on ederollora's answer and doing some research, I discovered that the call to angular.bootstrap() needs to be called after everything is defined. The above code becomes this:

$(document).ready(function() {
    var myApp = angular.module("myApp", []);

    myApp.controller("AppController",["$scope", "$http", function() {
        // so stuff with the $scope.
    }]);

    myApp.bootstrap(document, ["myApp"]); // compile the app last
});

Also, in the interest of migrating my app from jQuery to Angular, I change the document.ready calls to the angular version:

angular.element(document).ready(function() {
    var myApp = angular.module("myApp", []);

    myApp.controller("AppController",["$scope", "$http", function() {
        // so stuff with the $scope.
    }]);

    myApp.bootstrap(document, ["myApp"]); // compile the app last
});

It wasn't clear in the documentation that angular.boostrap() had to be called after defining everything, so I went and improved the documentation.

trusktr
  • 44,284
  • 53
  • 191
  • 263
  • *"By default, AngularJS doesn't seem to work unless I put it in the ``"* What do you mean? The `script` tag? That works fine in the `body`. Example: http://jsbin.com/zoyedeba/1 (source: http://jsbin.com/zoyedeba/1/edit). – T.J. Crowder Mar 30 '14 at 23:24
  • 1
    [AngularJS does not need to be placed in the HEAD](http://stackoverflow.com/questions/15538125/angularjs-in-head-vs-body)... – stuartd Mar 30 '14 at 23:25
  • Hmmm.... I need to find out why it doesn't work at the end of `` for me then. – trusktr Mar 30 '14 at 23:34
  • The problem all along was that I was calling `angular.bootstrap()` in the footer before defining my controllers and directives, which doesn't work. `bootstrap()` has to be called after everything is defined. This piece of information was missing from the AngularJS documentation for `bootstrap`, and I got it added in this pull request: https://github.com/angular/angular.js/pull/6921 – trusktr Apr 01 '14 at 04:12

1 Answers1

4

From AngularJS doc page:

Angular Tag

This example shows the recommended path for integrating Angular with what we call automatic initialization.

<!doctype html>
<html xmlns:ng="http://angularjs.org" ng-app>
  <body>
  ...
  <script src="angular.js">
  </body>
</html>
  1. Place the script tag at the bottom of the page. Placing script tags at the end of the page improves app load time because the HTML loading is not blocked by loading of the angular.js script. You can get the latest bits from http://code.angularjs.org. Please don't link your production code to this URL, as it will expose a security hole on your site. For experimental development linking to our site is fine.

The information was found: here

ederollora
  • 1,165
  • 2
  • 11
  • 29
  • Thanks! I'll post back when I find out why it doesn't work at the end of `` for me. – trusktr Mar 30 '14 at 23:34
  • yes please, and I'll edit my answer so that it better fits your question. – ederollora Mar 30 '14 at 23:35
  • which browser are you using? – ederollora Mar 30 '14 at 23:36
  • In this link you have information about Manual Initialization: http://docs.angularjs.org/guide/bootstrap#manual-initialization . Before manual initialization, there's some info about Automatic Initialization. Reading both topics should give you some advice – ederollora Mar 30 '14 at 23:40
  • aaaah. I found the problem. It works fine in the footer, as long as it is not inside `$(document).ready(function() {});`. Any idea why it won't work inside jQuery's document.ready function? – trusktr Mar 30 '14 at 23:40
  • Oooh. Do I need to do that same initialization in document.ready? – trusktr Mar 30 '14 at 23:41
  • As far as I know initilization, should not be placed in javascript. You should follow either the example I wrote you in my answer of the link about Manual Initilization – ederollora Mar 30 '14 at 23:42
  • No you don't need. In case you want to control EXACTLY when it is initialized then yes, you should add some javascript code. BUT if automatic initilization is OK for you then use Auto Initialization. It initializes as soon as DOM loads (DOM load event called DOMContentLoaded) or script tag is found. So placing the script tag `` right before `

    ` should be OK

    – ederollora Mar 30 '14 at 23:44
  • Thanks! Yeah, I'm fine with automatic initialization, however trying the manual initialization isn't working. I'm trying this: http://hastebin.com/qahuqikogi.xml – trusktr Mar 30 '14 at 23:57
  • have you tried exactly the code that manual initilization states in the docs? – ederollora Mar 31 '14 at 00:02
  • I saw the link, first `/path/to/angular.js` is just an example, or the actual code? – ederollora Mar 31 '14 at 00:03
  • That's just an example... but you can assume it is the actual angular.js library. Everything works fine when I let it be automatic, but if I wrap it in `angular.element(document).ready(function() {...});` and add the `angular.bootstrap()` line it won't work. I'm doing exactly as the demo. Here's my code verbatim. This works: http://hastebin.com/disecikegi.xml And this doesn't: http://hastebin.com/xugavupubo.xml – trusktr Mar 31 '14 at 00:12
  • I can't get manual initialization to work for the life of me. Here's my *entire* code: http://hastebin.com/xoxoniyaja.xml Here's the AngularJS error it's giving me: http://bit.ly/1dIuMY1 Seems like I did it like in the manual initialization doc, but of course I have way more code. – trusktr Mar 31 '14 at 02:22
  • Well I'm definetely not an experto on the field but could you pastebin (or any other) the exception trace you personally suffer from? – ederollora Mar 31 '14 at 08:25
  • However if you still got problems with manual init, I shouldn't worry too much since angularJS is enough intelligent to load the script as soon as the DOM is loaded or the script tag is reached. Then it could be more optimized than manual. – ederollora Mar 31 '14 at 08:26
  • I figured it out! I had to move the call to `angular.bootstrap()` to a position *after* setting up my controller ("RootController" in my case). So I guess `angular.bootstrap` compiles the app, which relies on having modules, directives, etc, all already configured before compilation happens. – trusktr Mar 31 '14 at 12:52
  • Good, please if you can edit my answer so that it becomes more complete adding your exact problems solution. We could help more people! – ederollora Mar 31 '14 at 16:58
  • So, "the recommended way" is not even used in the "get started" example? https://code.angularjs.org/0.10.6/docs-0.10.6/misc/started – serge May 09 '16 at 09:54
  • Developers guide: https://docs.angularjs.org/guide/bootstrap#angular-script-tag. Maybe getting started means just that. – ederollora May 09 '16 at 22:57