ngApp 不使用任何特定的模块名称 [英] ngApp without using any specific module name
问题描述
考虑下面的一段代码:
html 文件的一部分:
part of html file:
<body ng-app>
<div ng-controller="MainCtrl">{{name}}</div>
</body>
js 文件的一部分:
function MainCtrl($scope) {
$scope.name = "John";
}
我总是把我的控制器放在某个模块中,我在 ng-app 中定义了它的名字.当我没有定义任何模块时它是如何工作的?
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 有这个自动发现"功能,如果控制器在全局范围内定义,它允许它通过名称查找控制器.
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.
来自 Angular 的开发者指南:
From Angular's Developer Guide:
注意: 尽管 Angular 允许您在全局范围内创建控制器函数,但不建议这样做.在实际应用程序中,您应该为应用程序使用 Angular 模块的 .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.
这篇关于ngApp 不使用任何特定的模块名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!