Place ng-app in html tags

I am new to AngularJS. My teacher said that ng-app can be used in html, body, div, etc. tags. My question is, if ng app can use in html tag and body tag, is there any use if I use it in html tag and not body tag? I mean the title tag is middle body and html. Is there any effect in the headers when I use its html tag or not. hope my question is clear.

+3


source to share


3 answers


The reason why you would place the ng app inside the html tag above the body tag is because you want to manipulate something inside the head and body with angle symbols. Here's an example:

<html ng-app="MyApp">
<head>
  <meta charset="utf-8">
  <title ng-bind="'Title - ' + title"></title>
  <link rel="stylesheet" href="/css/style.css" />
</head>
<body>
...

      



Note that I have ng-bind in the header, so I can use the variable name title

to change the title of the web page the user is on. The title will always be Title - what ever link their on

In app.js, I make title

var a global variable. This is just one of the reasons why the ng app is put in the html tag instead of the body.

+6


source


Not. ng-app

does not affect <head>

if you do not intend to. It must be added to the scope

root element of your application html

.



In most cases, it is better to use it in a tag body

unless you want to change something in head

eg. title

... For dynamic values title

, ng-app

add to <html>

.

+5


source


yes there are some advantages if you put ng-app

in a tag <html>

,

<html>
    <head>
        <title></title>
    </head>
    <body>

    </body>
</html>

      

if you put it in html then you can control all things between html tags which means all html can be manipulated with angle characters, for EX you can change the title.

But if you put it in <body>

, you will lose the control <title>

or something inside the tags <head>

, and you can only control the tag <body>

.

here is DOC for NG-APP

Use this directive to automatically load your AngularJS app. The ngApp directive denotes the root element of the application and is usually located next to the root element of the page - for example, on the <body> or <html> tags.

+3


source







All Articles