0

wher should i use the $http in angularjs? in the controller or in the service? i have already implement it in the service, but i want to execute not at the start of the app, i want to execute after some user actions, is this possible in the service?

'use strict';

/* Services */

// Demonstrate how to register services
// In this case it is a simple value service.
angular
    .module('myApp.services')
    .service(
            'RestService',
            function($http, $log) {

                this.getERPProfile = function() {
                    var request = request;
                    request = JSON.stringify(request);


                    $http(
                            {
                                url : url,
                                method : "POST",
                                headers : {
                                    'Accept' : 'text/xml',
                                    'Content-Type' : '"text/xml; charset=\"utf-8\""'
                                },
                                dataType : 'xml',
                                data : request

                            }).success(
                            function(data, status, headers, config) {
                                var v1 = data;
                                return data;
                                $log.log(v1);
                            }).error(
                            function(data, status, headers, config) {
                                var v2 = status;
                                return data;
                                $log.log(v2);
                            });
                };

and has someone a good documentation about the difference of factory and service? the angulajs site does not help me to understand.

thanks for your help!

Eduard Gamonal
  • 7,883
  • 5
  • 37
  • 42
  • here is a good explination: http://stackoverflow.com/questions/15666048/angular-js-service-vs-provide-vs-factory – DanEEStar Jul 23 '13 at 09:07

1 Answers1

1

Services are instantiated, only once, the first time they are called. They're used to share logic and expose data between controllers, for example. Controllers are the glue between the view and the model, like in any MVC framework.

Running it at the start of the app or not has nothing to do with doing the $http request from a controller or from a service. Mae the http request when you need it.

Notice that controllers are instantiated when angular finds <div ng-controller=whatever>... . If your http request is there, it'll be triggered. There was a similar question about services and factories a few weeks ago and another one about injecting a service in a controller.

Community
  • 1
  • 1
Eduard Gamonal
  • 7,883
  • 5
  • 37
  • 42
  • hi, thanks for your quick answer! this means, i should not use the service for my requests? can you tell me about factory? whats the difference between service and factory? for what should i use both? thanks! – zualexander Jul 23 '13 at 09:15
  • service vs. factory http://stackoverflow.com/questions/13762228/confused-about-service-vs-factory/13763886#13763886 – alfrescian Jul 23 '13 at 09:33
  • @zualexander I suggest you read about controllers and services but not about factories for now. If you want to do the http request after a user action, I'd put it in a function in a controller. You can call either http or a method in a service that does it for you. services are used to share logic and data between controllers. – Eduard Gamonal Jul 23 '13 at 10:06
  • Hi, a nice example https://gist.github.com/whisher/6200561 I don't remember where I found it ^^ – Whisher Aug 10 '13 at 14:04
  • Hi @Wisher, thanks! however, in that example they're using global functions (they use `var` but I guess they're using it in a way that pollutes the global namespace) but I do like the comments that clarify the difference between service instance, factory and provider – Eduard Gamonal Aug 13 '13 at 06:35