ng-app 指令的位置(html vs body) [英] Placement of the ng-app directive (html vs body)
问题描述
我最近查看了一个用 angular 构建的 web 应用程序的代码,发现它是用放置在 ng-app="myModule" 指令编写的> 标签.在学习 angular 时,我只见过它用在
<html>
标签上,这是 angular docs 此处、此处和在他们的教程中.
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.
我自己对此进行了一些探索,发现了一些问题,特别是 这个和类似的这个,讨论加载多个一个页面的模块.但是,这种技术与我的情况不同,因为它涉及将 ng-app 放在 主体内的元素上,并使用手动引导程序同时运行两个 Angular 应用程序.
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.
据我所知,在 或
上使用
ng-app
的应用在运行时没有区别.代码>.据我了解,ng-app
指定了一个 Angular 应用程序的根,因此将它放置在 上会切断
超出了 angular 的范围,但我想不出这会影响事情的任何主要方式.所以我的问题是:将
ng-app
放在这些标签之一而不是另一个标签上有什么技术区别?
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?
推荐答案
ng-app
放在哪里没有太大区别.
There is no big difference where you put ng-app
.
如果你把它放在 <body>
上,那么你的 AngularJS 范围更小,稍微更快.
If you put it on <body>
then you have a smaller scope for AngularJS which is slightly faster.
但是我在 上使用了
ng-app
来操作
.
But I have used ng-app
on the <html>
for manipulating the <title>
.
这篇关于ng-app 指令的位置(html vs body)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!