为什么NG-应用不NG-模块? [英] Why ng-app not ng-module?

查看:134
本文介绍了为什么NG-应用不NG-模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,NG-应用程序初始化在AngularJS模块如下:

I understand that ng-app initializes a module in AngularJS as follows:

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

但是,当我教这人新的AngularJS或观看视频,在以上的应用程序和模块之间的术语不一致教官不可避免的摸索。 AngularJS是如此深思熟虑作为一个框架,我很惊讶,它并没有改变:

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 is so well thought out as a framework that I'm surprised it hasn't changed to:

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

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?

推荐答案

我不认为克雷​​格是问什么呢 NG-应用做还是怎么工作的。

I don't think Craig is asking what does ng-app do or how does it work.

我想他问为什么这样做,创建角名称指令 NG-应用的人们。他们为什么不将其命名为 NG-模块 NG-模块会更容易理解了。

I think he's asking why did the people that created angular name that directive ng-app. Why didn't they name it ng-module. ng-module would be easier to understand.

例如 NG-控制器应命名一个控制器, NG-模块应命名一个模块。在方法来创建它们被命名为模块()控制器(),不存在所谓的应用程序。

For example ng-controller should name a controller, ng-module should name a module. The angular methods to create them are named module() and controller(), there is no method or entity called "app".

我倾向于克雷格同意。这就是说,如果我是去猜测为什么他们把它命名为 NG-应用我想那是因为你只允许有一个 NG-应用指令在你的HTML。如果你想和你的HTML页面上有多个模块,可以通过编程方式做到这一点。

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. If you wanted to have more than one module associated with your HTML page you can do it programmatically.

所以 NG-应用更多的是实用与模块来引导你的HTML,它不是模块与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.

如果你看看,这就是它暗示的文档:

If you look at the documentation that's what it suggests:

使用此指令自动引导的AngularJS应用。该
  ngApp指令指定应用程序的根元素,并且
  典型地放置在页面的根元素近 - 例如在
   或标记。

Use this directive to auto-bootstrap an AngularJS application. The ngApp directive designates the root element of the application and is typically placed near the root element of the page - e.g. on the or tags.

只有一个AngularJS应用程序可以自动自举每HTML
  文件。在文档中找到的第一个ngApp将用于定义
  根元素自动引导作为一个应用程序。运行多个
  HTML文档中的应用程序,您必须手动引导他们
  使用angular.bootstrap代替。 AngularJS应用不能
  相互嵌套。

Only one AngularJS application can be auto-bootstrapped per HTML document. The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application. To run multiple applications in an HTML document you must manually bootstrap them using angular.bootstrap instead. AngularJS applications cannot be nested within each other.

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

所有这一切说,如果你想要一个 NG-模块指令,你可以编写自己的包裹 angular.bootstrap()的功能。你可以找到更多的细节以及如何做到这一点的一篇博客文章中,我写这件事code:<一href=\"http://www.simplygood$c$c.com/2014/04/angularjs-getting-around-ngapp-limitations-with-ngmodule/\"相对=nofollow> AngularJS:四处逛逛ngApp局限性ngModule

All that said if you want an ng-module directive you could always write your own to wrap the angular.bootstrap() function. 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

这篇关于为什么NG-应用不NG-模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆