angularJS 模块声明中空数组的含义
Meaning of the empty array in angularJS module declaration
在我的 中,我了解到代码 var app = angular.module('myApp', []);
将模块 app
连接到视图 myApp
。
我想知道为什么我们在模块声明中有空数组 []
。
空数组有什么用?
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
指令。
例子
假设你有一个应用程序,它有不同的组件,如服务、工厂、过滤器、指令等。你可以为它们中的每一个创建一个模块。只是为了分离关注点。
angular.module('myApp.filters', [])
angular.module('myApp.services', [])
angular.module('myApp.controllers', [])
angular.module('myApp.directives', [])
angular.module('myApp.constants', [])
并且在向其附加组件时,您可以简单地使用它的特定模块。就像您想添加服务一样,您只需通过
将该服务添加到 myApp.services
angular.module('myApp.services').service('example', function(){
})
然后在你的内部 app.js 你可以将所有这些模块组合成一个模块,如下所示。
angular.module('myApp', [
'myApp.services',
'myApp.controllers',
'myApp.directives',
'myApp.directives',
'myApp.constants'
])
在初始化应用程序时,您可以在内部简单地使用 myApp
,这将使所有其他模块都可用。
What is the empty array used for?
在您的代码中,您正在创建一个不注入任何依赖项的模块,[]
这意味着它独立于任何其他 angular
模块。
Dependency injected inside the []
is nothing but importing module
angular.module('app', [])
是创建一个没有任何模块依赖的新模块。
angular.module('app')
是检索名称为 app
.
的现有模块
Angulars API 说 Passing one argument retrieves an existing angular.Module, whereas passing more than one argument creates a new angular.Module
https://docs.angularjs.org/api/ng/function/angular.module
在我的 var app = angular.module('myApp', []);
将模块 app
连接到视图 myApp
。
我想知道为什么我们在模块声明中有空数组 []
。
空数组有什么用?
That array is meant to add various module to your current
app
which is mentioned in your first part ofangular.module
as string`, You could simply say for injecting various dependency.
您可以在您的应用程序中为 angular
的每个组件创建 n
个模块,然后您可以将它们组合成一个应用程序,然后您可以 angular.bootstrap
或应用它在页面上使用 ng-app
指令。
例子
假设你有一个应用程序,它有不同的组件,如服务、工厂、过滤器、指令等。你可以为它们中的每一个创建一个模块。只是为了分离关注点。
angular.module('myApp.filters', [])
angular.module('myApp.services', [])
angular.module('myApp.controllers', [])
angular.module('myApp.directives', [])
angular.module('myApp.constants', [])
并且在向其附加组件时,您可以简单地使用它的特定模块。就像您想添加服务一样,您只需通过
将该服务添加到myApp.services
angular.module('myApp.services').service('example', function(){
})
然后在你的内部 app.js 你可以将所有这些模块组合成一个模块,如下所示。
angular.module('myApp', [
'myApp.services',
'myApp.controllers',
'myApp.directives',
'myApp.directives',
'myApp.constants'
])
在初始化应用程序时,您可以在内部简单地使用 myApp
,这将使所有其他模块都可用。
What is the empty array used for?
在您的代码中,您正在创建一个不注入任何依赖项的模块,[]
这意味着它独立于任何其他 angular
模块。
Dependency injected inside the
[]
is nothing but importing module
angular.module('app', [])
是创建一个没有任何模块依赖的新模块。
angular.module('app')
是检索名称为 app
.
Angulars API 说 Passing one argument retrieves an existing angular.Module, whereas passing more than one argument creates a new angular.Module
https://docs.angularjs.org/api/ng/function/angular.module