简体   繁体   English

为什么ng-app不是ng-module?

[英]Why ng-app not ng-module?

I understand that ng-app initializes a module in AngularJS as follows: 我知道ng-app在AngularJS中初始化模块如下:

var app = angular.module('myApp', []);
<html ng-app="myApp">

But when I teach this to someone new to AngularJS or watch a video, instructors inevitable fumble over the inconsistency in the terminology between app and module. 但是,当我向刚接触AngularJS的人教导或观看视频时,教师不可避免地会在应用程序和模块之间的术语不一致中摸索。 AngularJS is so well thought out as a framework that I'm surprised it hasn't changed to: AngularJS是一个经过深思熟虑的框架,我很惊讶它没有变成:

var app = angular.app('myApp', []);
<html ng-app="myApp">

OR 要么

var app = angular.module('myModule', []);
<html ng-module="myModule">

Has anyone been following the project long enough to know the history on this part of the framework? 有没有人一直关注这个项目,知道框架这一部分的历史?

I don't think Craig is asking what does ng-app do or how does it work. 我不认为Craig会问ng-app做了什么或者它是如何工作的。

I think he's asking why did the people that created angular name that directive ng-app . 我想他在问为什么创建角度名称的人指导ng-app Why didn't they name it ng-module . 他们为什么不把它命名为ng-module ng-module would be easier to understand. ng-module更容易理解。

For example ng-controller should name a controller, ng-module should name a module. 例如, ng-controller应该命名一个控制器, ng-module应该命名一个模块。 The angular methods to create them are named module() and controller() , there is no method or entity called "app". 创建它们的angular方法名为module()controller() ,没有名为“app”的方法或实体。

I tend to agree with Craig. 我倾向于同意克雷格。 That said if I were go speculate why they named it ng-app I would think it's because you are only allowed to have one ng-app directive in your HTML. 那说如果我去推测为什么他们将它命名为ng-app我会认为这是因为你只允许在你的HTML中有一个ng-app指令。 If you wanted to have more than one module associated with your HTML page you can do it programmatically. 如果您希望将多个模块与HTML页面关联,则可以通过编程方式执行此操作。

So ng-app is more of a utility to bootstrap your HTML with a module, it is not a generic way to associate modules with your HTML. 因此, ng-app更像是用模块引导HTML的实用程序,它不是将模块与HTML相关联的通用方法。

If you look at the documentation that's what it suggests: 如果你看一下它建议的文档:

Use this directive to auto-bootstrap an AngularJS application. 使用此伪指令自动引导AngularJS应用程序。 The ngApp directive designates the root element of the application and is typically placed near the root element of the page - eg on the or tags. ngApp指令指定应用程序的根元素,通常放在页面的根元素附近 - 例如在或标记上。

Only one AngularJS application can be auto-bootstrapped per HTML document. 每个HTML文档只能自动引导一个AngularJS应用程序。 The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application. 在文档中找到的第一个ngApp将用于定义作为应用程序自动引导的根元素。 To run multiple applications in an HTML document you must manually bootstrap them using angular.bootstrap instead. 要在HTML文档中运行多个应用程序,必须使用angular.bootstrap手动引导它们。 AngularJS applications cannot be nested within each other. AngularJS应用程序不能互相嵌套。

http://docs.angularjs.org/api/ng/directive/ngApp http://docs.angularjs.org/api/ng/directive/ngApp

All that said if you want an ng-module directive you could always write your own to wrap the angular.bootstrap() function. 所有这一切都说如果你想要一个ng-module指令,你总是可以编写自己的指令来包装angular.bootstrap()函数。 You can find more details and code about how to do this on a blog post I wrote about it: AngularJS: Getting around ngApp limitations with ngModule 您可以在我撰写的博客文章中找到有关如何执行此操作的更多详细信息和代码: AngularJS:使用ngModule解决ngApp限制问题

ng-app means: That page has Angular in it! ng-app表示:该页面中包含Angular!

ng-app="module" means: That page has Angular in it and necessary controls/services/etc are defined in that module. ng-app="module"表示:该页面中包含Angular,并且该模块中定义了必要的控件/服务/等。

ng-app defines the main or bootstrap module of your application, which should perform the initialization task of your application. ng-app定义ng-appmain模块或bootstrap模块,它应该执行应用程序的初始化任务。 There may be case where at run time you want to decide what which should be the main module of your application. 在某些情况下,您可能希望确定应用程序的主要模块应该是什么。 Like in java you have many methods and classes but you define one main method as starting point. 就像在java中一样,您有许多方法和类,但是您将一个main方法定义为起点。 Similarly, in angular you have many module, however, you define one module as the starting point of application. 类似地,在角度上你有很多模块,但是,你将一个模块定义为应用的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM