考虑下面的一块code的:
Consider the piece of code below:
HTML文件的一部分:
part of html file:
<body ng-app>
<div ng-controller="MainCtrl">{{name}}</div>
</body>
js文件的一部分:
part of js file:
function MainCtrl($scope) {
$scope.name = "John";
}
我总是把我的控制器在某些模块中,他的名字我定义NG-应用程序。它是如何工作的时候,我不定义任何模块?
I always put my controllers in some module, whose name I define in ng-app. How it works when I don't define any module ?
棱角分明,具有这种自动发现功能,允许如果他们在全球范围内将它定义为通过其名称查找控制器。
Angular, has this "auto-discover" feature, that allows it to look up controllers by their name if they are defined on the global scope.
此功能主要用于快速演示/样机/验证的概念片段,而不是现实世界的应用程序。
This feature is mainly intended for quick demos/prototypes/proof-of-concept snippets and not real-world applications.
从角的 开发者指南
From Angular's Developer Guide:
注意:虽然角度可以让你在全球范围内建立控制器功能,不建议这样做。在实际应用中,你应该用你的角模块的 .controller
方法为您的应用[...]
NOTE: Although Angular allows you to create Controller functions in the global scope, this is not recommended. In a real application you should use the .controller
method of your Angular Module for your application [...]
对于空 ngApp
,如果没有指定名称,它只是意味着没有模块分配控制器,指令服务,但比一切其他正常工作。
Regarding the empty ngApp
, if there is no name specified, it only means that there is no module to assign controllers, directives, services to, but other than that everything works fine.