AngularJs(Part 4)
来源:互联网 发布:淘宝上卖画 编辑:程序博客网 时间:2024/06/05 17:05
Modules depending on other Modules
Angular does an excellent job of managing object dependencies. it can even take care of module dependencies.
So we can easily group releated services into one module, and make other modules dependent on it.
angular.module('my',['my1','my2']);
the preceding code create a module named "my" which dependent on two other modules "my1" and "my2".
So modules can dependent on other modules and services can dependent on other services.
and this raises several interesting questions, which are as follows:
1. Can a service defined in one AngularJS module depend on services in another module?
2. Can services defined in a child module depend on a service in a parent module, or only on services defined in child modules?
3. Can we have module-private services visible only in a certain module?
4. Can we have several services with the same name defined in different modules?
angular.module('app',['engines'])
.factory('car',function(dieselEngine){
return {
info:function(){
console.log(dieselEngine.type);
}
};
});
angular.module('engines',[])
.factory('dieselEngine',function(){
return {
type:'a sport car'
};
});
above code can execute with no problems.
what's more surprising is that services defined on sibling modules are also visible to each other. check below code snippet out:
angular.module('my',['car','engines']);
angular.module('car',[])
.factory('carlog',function(dieselEngine){
return {
info:function(){
console.log(dieselEngine.type);
}
};
});
angular.module('engines',[])
.factory('dieselEngine',function(){
return {
type:'a sport car'
};
});
conclusion: a service defined in one of the application's modules is visible to all the other modules. In other words,
hierarchy of modules doesn't influence services' visibility to other modules. when Angujar bootstraps an application,
it combines all the services defined across all the modules into one application, that is , global namespace.
Since Angular combines all the services from all modules into one big namespace.
there can be only one service with a given name.
currently, there is no way to restrict service's visibility to the other modules.
Angular does an excellent job of managing object dependencies. it can even take care of module dependencies.
So we can easily group releated services into one module, and make other modules dependent on it.
angular.module('my',['my1','my2']);
the preceding code create a module named "my" which dependent on two other modules "my1" and "my2".
So modules can dependent on other modules and services can dependent on other services.
and this raises several interesting questions, which are as follows:
1. Can a service defined in one AngularJS module depend on services in another module?
2. Can services defined in a child module depend on a service in a parent module, or only on services defined in child modules?
3. Can we have module-private services visible only in a certain module?
4. Can we have several services with the same name defined in different modules?
angular.module('app',['engines'])
.factory('car',function(dieselEngine){
return {
info:function(){
console.log(dieselEngine.type);
}
};
});
angular.module('engines',[])
.factory('dieselEngine',function(){
return {
type:'a sport car'
};
});
above code can execute with no problems.
what's more surprising is that services defined on sibling modules are also visible to each other. check below code snippet out:
angular.module('my',['car','engines']);
angular.module('car',[])
.factory('carlog',function(dieselEngine){
return {
info:function(){
console.log(dieselEngine.type);
}
};
});
angular.module('engines',[])
.factory('dieselEngine',function(){
return {
type:'a sport car'
};
});
conclusion: a service defined in one of the application's modules is visible to all the other modules. In other words,
hierarchy of modules doesn't influence services' visibility to other modules. when Angujar bootstraps an application,
it combines all the services defined across all the modules into one application, that is , global namespace.
Since Angular combines all the services from all modules into one big namespace.
there can be only one service with a given name.
currently, there is no way to restrict service's visibility to the other modules.
0 0
- AngularJs(Part 4)
- AngularJs(Part 1)
- AngularJs(Part 2)
- AngularJs(Part 3)
- AngularJs(Part 5)
- AngularJs(Part 6)
- AngularJs(Part 7)
- AngularJs(Part 8)
- AngularJs(Part 9)
- AngularJS(Part 10)
- 创建自定义 AngularJS 指令:Part 4 transclude和restrict介绍
- 创建自定义 AngularJS 指令:Part 5 link
- PART 4
- 创建自定义 AngularJS 指令:Part I 指令基础知识
- 创建自定义 AngularJS 指令:Part 2 孤立作用域
- 创建自定义 AngularJS 指令:Part 6 使用controllers
- “JavaScript Promises和AngularJS $q Service”Part 1 (基础篇)
- “JavaScript Promises和AngularJS $q Service”Part 2 (教程篇)
- Android Framework------之PowerManagerService的功能
- -ms-touch-action
- Shell脚本语法规则
- AngularJs(Part 3)
- HDU5100 Chessboard(数学题)
- AngularJs(Part 4)
- Java AOP 之 AspectJ
- hdu4907(小技巧)
- android中调用软键盘
- 2008~2014,这6年我都学了啥干了啥
- C++强制类型转换
- Java包一般性介绍和其他说明
- putty源码编译,去掉烦人的弹框
- vcs dump vpd的方法