简体   繁体   中英

AngularJS get factory from different module

Module 1:

angular.module('mod1', [])
   .factory('mod1Fac', function(){
        return {
          test: function (){
              return('this is from mod1');
          }
        };
   };

Module 2:

angular.module('mod2', ['mod1'])
   .controller('mod2Ctrl', function (mod1Fac, $scope){
       $scope.string = mod1Fac.test();
   }

What changes to this code do I need to make it so I can access the data from mod1's factory into mod2's controller?

You just have to inject $scope ,

angular.module('mod2', ['mod1'])
   .controller('mod2Ctrl', function (mod1Fac,$scope){
       $scope.string = mod1Fac.test();
   }

here is the working Plunker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM