Using a factory inside another factory AngularJS

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

I have a module...

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

And then a factory

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


        
相关标签:
2条回答
  • 2021-02-12 19:55

    This is what i did and worked fine. Call SessionPresenters from Session

    angular.module('tryme3App')
        .factory('Session', function ($resource, DateUtils, SessionPresenters) {
            return $resource('api/sessions/:id', {}, {
                'query': { method: 'GET', isArray: true},
                'get': {
                    method: 'GET',
                    transformResponse: function (data) {
                        data = angular.fromJson(data);
    
                        var result = SessionPresenters.get({id: data.id})
                        data.presenters = result; 
                        return data;
                    }
                },
                'update': { method:'PUT' }
            });
        }).factory('SessionPresenters', function ($resource, DateUtils) {
            return $resource('api/session.Presenters/:id', {}, {
                'query': { method: 'GET', isArray: true},
                'get': {
                    method: 'GET', isArray: true
                },
                'update': { method:'PUT' }
            });
        });
    
    0 讨论(0)
  • 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;
            }
        ]);
    
    0 讨论(0)
提交回复
热议问题