Using a factory inside another factory AngularJS

后端 未结 2 907
萌比男神i
萌比男神i 2021-02-12 18:58

I have a module...

angular.module(\'myModule\', []);

And then a factory

angular.module(\'myModule\')
.factory(\'factory1\', [
          


        
2条回答
  •  萌比男神i
    2021-02-12 19:59

    This is what I would do:

    On Factory One

    angular.module('sampleApp')
        .factory('factory1', function() {
            var factory1 = {};
    
            factory1.method1 = function () {
                return true;
            };
    
            factory1.method2 = function () {
                return "hello";
            };
    
            return factory1;
        }
    );
    

    On Factory Two

    angular.module('sampleApp')
        .factory('factory2', ['factory1',
            function(factory1) {
    
                var factory2 = {};
    
                factory2.method3 = function () {
                    return "bye vs " + factory1.method2();
                };
    
                return factory2;
            }
        ]);
    

提交回复
热议问题