4

I've got an AngularJS model in which I've created a module called myService to hold some commonly used code which I use throughout my application. My Common factory is where I've been adding all my methods and now I want to split this up and give them good names.

A lot of my methods call each other so how can I call a method which is in another factory?

angular.module('myService', ['ngResource'])
  .factory('test2', ($window) ->
    return {
      foobar: () ->
        Common.test()
    }
  )  
  .factory('Common', ($window) ->
    return {
      test: () ->
        alert 'testing'
    }
  )

2 Answers 2

12

You only need to inject it:

.factory('test2', function (Common) {
  return {
    foobar: function () {
      Common.test();
    }
  };
})
Sign up to request clarification or add additional context in comments.

Comments

0

This is what i did and worked fine. Call SessionPresenters from Session. You need to pass the SessionPresenters as an argument to factory implementation function.

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' }
        });
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.