看一下下面的代码。
主要应用程式:
(function (){
var app = angular.module("myApp",["aSubApp","anotherSubApp"]);
var aController = function($scope, subService){
subService.childService();
}
aController.$inject = ["$scope","subService"];
app.controller("testController",aController);
app.service("parentService",function(){
this.parentalService = function(){ alert("Parenting Services"); }
});
})();
现在,子模块...
子模块1:
(function () {
'use strict';
var aSubApp = angular.module("aSubApp", []);
var subService = function (parentService, anotherService) {
this.childService = function () {
alert("Can I call random services???. I Don't know if they exist");
parentService.parentalService();
// This doesn't call the service defined beneath.
anotherService.aRandomService();
alert("Looks like they do!.");
}
}
var anotherService = function () {
this.aRandomService = function () {
alert("another random SubService!!!");
}
}
aSubApp.service("anotherService", anotherService);
subService.$inject = ["parentService", "anotherService"];
aSubApp.service("subService", subService);
})();
子模块2:
(function () {
'use strict';
var anotherSubApp = angular.module("anotherSubApp", []);
var anotherService = function () {
this.aRandomService = function () {
alert("A random SubService!!!");
}
}
anotherSubApp.service("anotherService", anotherService);
})();
为什么subService.childService中的代码可以工作?它不应该告诉我,它不知道在哪里存在parentService和anotherService进行注入吗?既然它们不是从属模块?是JS吗?
var app = angular.module("myApp",["aSubApp","anotherSubApp"]);
您正在主应用程序中进行注入aSubApp
和anotherSubApp
模块化,因此dependent
您也可以使用这些服务myApp
。
如果您查看自己index.html
的myApp
模块,您会发现所有JS文件都包含在其中[subService,parentService,anotherService]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句