看看下面的代碼。角度調用隨機服務!爲什麼?
主要應用:
(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);
})();
子單詞數:
(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的東西?
「您正在向主應用程序注入aSubApp和anotherSubApp模塊,因此依賴服務也可用於myApp。」 - 我接受,但爲什麼'anotherSubApp'在'aSubApp'中可用,這我不能幻想! – anchreg
@anchreg:因爲您在aSubApp,aSubApp.service(「anotherService」,anotherService);你在這裏聲明另一個服務... – Thalaivar
但是,我的意思是我的模塊aSubApp中的另一個服務。我得到的警報來自anotherSubApp的另一個服務,它是aSubApp不知道的! – anchreg