angularJS模块声明中空数组的含义 [英] Meaning of the empty array in angularJS module declaration

查看:115
本文介绍了angularJS模块声明中空数组的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的之前的问题中,我了解到代码 var app = angular.module('myApp',[]); 将模块 app 连接到视图 myApp

In my previous question, I understand that the code var app = angular.module('myApp', []); connects the module app to the view myApp.

我想知道为什么我们有空数组 [] 在模块声明中。

I would like to know why we are having the empty array [] in the module declaration.

用于的空数组是什么?

推荐答案


该数组旨在将各种模块添加到当前的应用程序,其中提到
angular.module 的第一部分为字符串`,你可以简单地说
注入各种依赖。

That array is meant to add various module to your current app which is mentioned in your first part of angular.module as string`, You could simply say for injecting various dependency.

你可以在你的应用程序中为 angular n 模块数量>&然后你可以将它们组合成一个单独的应用程序,你可以 angular.bootstrap 或使用 ng-app 在页面上应用它指令。

You could create an n number of module inside your app for each component of angular & then you could combine them into one single app and you can angular.bootstrap or apply it on page using ng-app directive.

示例

假设您有一个具有不同组件的应用程序服务,工厂,过滤器,指令等。您可以为每个服务创建一个模块。只是为了分离关注点。

Suppose you have an app which has different component like services, factory, filters, directives, etc. You could create a module for each of them. Just for making separation of concern.

angular.module('myApp.filters', [])
angular.module('myApp.services', [])
angular.module('myApp.controllers', [])
angular.module('myApp.directives', [])
angular.module('myApp.constants', [])

在附加组件时你可以只需使用它的特定模块。就像你想要添加服务一样,你只需将该服务添加到 myApp.services

And while appending an component to it you could simply use that specific module of it. Like you wanted to add service then you just add that service into myApp.services by doing

angular.module('myApp.services').service('example', function(){

})

然后在你的app.js中你可以将所有这些模块组合成一个模块,如下所示。

And then inside you app.js you could do combine all of this modules to an single module like below.

angular.module('myApp', [
     'myApp.services', 
     'myApp.controllers', 
     'myApp.directives', 
     'myApp.directives', 
     'myApp.constants'
])

在初始化应用程序时,您只需在里面使用 myApp ,这将使所有其他模块可用。

While initializing an app you could simply use myApp inside, that would make available all other module to it.


用于的空数组是什么?

What is the empty array used for?

在你的代码中你创建了一个模块不会注入任何依赖, [] 这意味着它独立于任何其他 gular 模块。

In you code you are creating an module which doesn't inject any dependency, [] which means it is independent of any other angular module.


[] 只是导入模块

这篇关于angularJS模块声明中空数组的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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