AngularJS access service from different module

后端 未结 3 1635
我在风中等你
我在风中等你 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:24

    after made some changes html should look like:

    
    

    Above section of code used to bootstrap your angular module

    angular should look like:

     var myModule = angular.module('myModule', ['module1','module2']);
        myModule.controller("appservices",["$scope","mod1factory","mod2factory",function($scope,mod1factory,mod2factory){
    
           console.log(mod1factory.getData()) ;
           console.log(mod2factory.getData()) ;
        }]);
        var mod1 = angular.module('module1',[]);
        mod1.factory("mod1factory",function(){
            var mod1result = {};
            mod1result = {
                getData: function(){
                    return "calling module 1 result";
                }
            }
            return mod1result;
        });
        var mod2 = angular.module('module2',[]);
        mod2.factory("mod2factory",function(){
            var mod2result = {};
            mod2result = {
                getData: function(){
                    return "calling module 2 result";
                }
            }
            return mod2result;
        });
    

    Explanation: created a main module myModule and inject other modules(in my case module1 and module2) as dependency so by this you can access both the module inside the main module and share the data between them

    console.log(mod1factory.getData()) ;
    console.log(mod2factory.getData()) ;
    

    created two factory and inject it in my controller mod1factory and mod12factory in my case . so mod1 & mod2 are both differnt modules but can share info. inside main controller myModule

提交回复
热议问题