次のコードを見てください。角度コールランダムサービス!どうして?
主なアプリケーション:今
(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のことですか?
"メインアプリケーションにaSubAppとanotherSubAppモジュールを注入しているので、従属サービスもmyAppで利用できます。" - これは私が受け入れるが、なぜ 'anotherSubApp'は 'aSubApp'で利用できるのか、これは私が想像することはできない! – anchreg
@anchreg:aSubAppにこれがあるので、aSubApp.service( "anotherService"、anotherService);あなたはここに別のサービスを宣言しています... – Thalaivar
しかし、私は私のモジュールaSubAppのanotherServiceを意味しました。私が得る警告はanotherSubAppのanotherServiceからのものです。これはaSubAppが知らない! – anchreg