Service with multiple functions

23 views
Skip to first unread message

mark goldin

unread,
Nov 24, 2014, 1:38:53 PM11/24/14
to ang...@googlegroups.com
I am creating a service in the following way:

explorer.factory("service", function ($http, $q) {
    return {
        getData : function(params){      
            ........
            ........      
            return value;
        }
    }
});

What if I want to have more than one function in my service?

Thanks

Michael Bielski

unread,
Nov 24, 2014, 1:50:27 PM11/24/14
to ang...@googlegroups.com
You have two ways that you can do this:

explorer.factory("service", function ($http, $q) {
    var myService = {
        functionA:function(){return something;},
        functionB:function(){return someOtherthing;}
    };

    return myService;
});

or you can do this:

explorer.factory("service", function ($http, $q) {
    functionA:function(){return something;}

    functionB:function(){return someOtherthing;}


    return {
         doThis:functionA,
         doThat:functionB
    };
});

Either way works.

mark goldin

unread,
Nov 24, 2014, 1:59:07 PM11/24/14
to ang...@googlegroups.com
Yes, that seems to be working fine. Thanks!

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/uikiXDokAX4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages