Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
555 views
in Technique[技术] by (71.8m points)

angularjs - Placement of the ng-app directive (html vs body)

I recently reviewed the code for a webapp built with angular and found that it was written with the ng-app="myModule" directive placed on the <body> tag. When learning angular, I've only ever seen it used on the <html> tag, as recommended by the angular docs here, here, and in their tutorial.

I've explored this a bit on my own and found SO questions, notably this one and similarly this one, that discuss loading multiple modules for a page. However, this technique different from my case, as it involves placing ng-app on elements within the body and using manual bootstrapping to run two angular apps at the same time.

As far as I can tell, there is no difference at runtime between an app with ng-app on <html> or <body>. As I understand it, ng-app designates the root of an angular application, so placement of it on the <body> would cut <head> out of angular's scope, but I can't think of any major way this would affect things. So my question is: What are the technical difference between placing ng-app on one of these tags instead of the other?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no big difference where you put ng-app.

If you put it on <body> then you have a smaller scope for AngularJS which is slightly faster.

But I have used ng-app on the <html> for manipulating the <title>.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...