jsh*_*303 8 javascript dependency-injection module angularjs angularjs-module
在我之前的问题中,我理解代码var app = angular.module('myApp', []);将模块连接app到视图myApp.
我想知道为什么我们[]在模块声明中有空数组.
什么是空数组?
Pan*_*kar 15
该数组旨在为您的当前添加各种模块,
app这在您的第一部分angular.module作为字符串`中提到,您可以简单地说注入各种依赖.
您可以n在应用程序内为每个组件创建一些模块angular,然后您可以将它们组合到一个单独的应用程序中,您可以angular.bootstrap使用ng-app指令在页面上应用它.
例
假设您的应用程序具有不同的组件,如服务,工厂,过滤器,指令等.您可以为每个组件创建一个模块.只是为了分离关注点.
angular.module('myApp.filters', [])
angular.module('myApp.services', [])
angular.module('myApp.controllers', [])
angular.module('myApp.directives', [])
angular.module('myApp.constants', [])
Run Code Online (Sandbox Code Playgroud)
在向其添加组件时,您可以简单地使用它的特定模块.就像你想添加的服务,然后你只需要添加该服务将myApp.services通过做
angular.module('myApp.services').service('example', function(){
})
Run Code Online (Sandbox Code Playgroud)
然后在app.js内部,您可以将所有这些模块组合到一个模块中,如下所示.
angular.module('myApp', [
'myApp.services',
'myApp.controllers',
'myApp.directives',
'myApp.directives',
'myApp.constants'
])
Run Code Online (Sandbox Code Playgroud)
在初始化应用程序时,您可以简单地myApp在内部使用,这将使所有其他模块可用.
什么是空数组?
在您的代码中,您正在创建一个不注入任何依赖关系的模块,[]这意味着它独立于任何其他angular模块.
注入内部的依赖
[]只是导入模块
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
| 归档时间: |
|
| 查看次数: |
4356 次 |
| 最近记录: |