Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS in HEAD vs BODY

In all of the AngularJS examples, the Angular library is placed in the HEAD tags of the document. I have an existing project that has been built upon the HTML5 Boilerplate layout. This defines that JS libraries should be placed at the very bottom of the DOM before the </BODY> tag.

Does AngularJS need to be placed in the HEAD?

like image 541
Cadriel Avatar asked Mar 21 '13 01:03

Cadriel


People also ask

What is the difference between head and body?

The <body> encapsulates the contents of the document, while the <head> part contains meta elements, i.e., information about the contents.

What is the difference between JavaScript in head and body?

JavaScript in head: A JavaScript function is placed inside the head section of an HTML page and the function is invoked when a button is clicked. JavaScript in body: A JavaScript function is placed inside the body section of an HTML page and the function is invoked when a button is clicked.

Why do we write JavaScript code in body and head tag?

Put your functions in the head section, this way they are all in one place, and they do not interfere with page content. Scripts in <body> If you don't want your script to be placed inside a function, or if your script should write page content, it should be placed in the body section.

What is the difference when the script tag appears in the HTML body or head sections?

When you place a script in the head section, you will ensure that the script is loaded before anyone uses it. Scripts in the body section: Scripts to be executed when the page loads go in the body section. When you place a script in the body section it generates the content of the page.


2 Answers

AngularJS does not need to be placed in the HEAD, and actually you normally shouldn't, since this would block loading the HTML.

However, when you load AngularJS at the bottom of the page, you will need to use ng-cloak or ng-bind to avoid the "flash of uncompiled content". Note that you only need to use ng-cloak/ng-bind on your "index.html" page. When ng-include or ng-view or other Angular constructs are used to pull in additional content after the initial page load, that content will be compiled by Angular before it is displayed.

See also https://stackoverflow.com/a/14076004/215945

like image 80
Mark Rajcok Avatar answered Sep 19 '22 23:09

Mark Rajcok


This one answer on Google Groups gave me perfect insight (shortened):

It really depends on the content of your landing page. If most of it is static with only few AngularJS bindings than yes, I agree, the bottom of the page is the best. But in case of a fully-dynamic content you would like to load AngularJS ASAP [in the head].

So if your content is generated in large part through Angular, you'd save yourself the extra CSS and ng-cloak directives by just including Angular in the head.

https://groups.google.com/d/msg/angular/XTJFkQHjW5Y/pbSotoaqlkwJ

like image 33
yerforkferchips Avatar answered Sep 17 '22 23:09

yerforkferchips