AngularJS access service from different module

后端 未结 3 1640
我在风中等你
我在风中等你 2020-12-14 07:13
\'use strict\';

var trkiApp = angular.module(\'trkiApp\', [ 
  \'trkiApp.tStatus\', 
  \'trkiApp.feed\'
]);



var tStatus = angular.module(\'trkiApp.tStatus\', [])         


        
3条回答
  •  太阳男子
    2020-12-14 07:25

    A Module is a collection of configuration and run blocks which get applied to the application during the bootstrap process. Modules can list other modules as their dependencies. Depending on a module implies that required module needs to be loaded before the requiring module is loaded.

    var myModule = angular.module('myModule', ['module1','module2']);
    

    When you injected your module, the services got registered during the configuration phase and you could access them, so to make a long story short, it's the correct behavior and the core fundamentals of dependency injection in Angular. For example

    angular.module('module1').service('appservice', function(appservice) {
       var serviceCall = $http.post('api/getUser()',"Role");
    });
    

    So how it can be accessed using angular.module('myModule');

    angular.module('myModule').controller('appservice', function(appservice)
    {
        var Servicedata= appservice.ServiceCall('role');
    }
    

    This how it can be accessed. If anyone has another suggestion please say so.

提交回复
热议问题