3

As you may know, AngularJS $http service is allowing to call it with/out specific function, for ex:

  1. $http(req).then(function(){...}, function(){...});

  2. $http.get('/someUrl', config).then(successCallback, errorCallback);

I would like to get some more information about the way I can implement it on my factory and generally in JS.

2
  • If question is more about how to architecture angular data retrieval - check the $resource - docs.angularjs.org/api/ngResource/service/$resource Commented Nov 23, 2015 at 9:22
  • To me it seems more like you asking about how to implement callbacks. Is that what you asking? Commented Nov 23, 2015 at 9:49

2 Answers 2

2

Functions are Objects in JavaScript. This means that you can assign other properties and functions on a function.

function foo(){
   //do something
}

foo.bar = function(){
   //do something else
}
1
  • Thank you very much! make sense. Example for z0r: var a = function(){ alert('main'); } a.foo = function(){ alert('bar'); } a(); a.foo(); Commented Nov 23, 2015 at 9:47
0

As it was mentioned above you can implement what you want using Angular's '$resource'. Here is an example of how it can be used:

app.service('testResource', ['$resource',  function ($resource) {
    var apiBaseUrl = 'http://test-backend/api';
    var testResource = $resource(
        apiBaseUrl + '/test/'
        {},
        {
            'query': {
                method: 'GET',
                isArray: true
            }
        }
    );
    this.getAll = function () {
        return testResource
            .query()
            .$promise
            .then(function (data) {
                var tests = [];
                angular.forEach(data[0], function (value) {
                    tests.push(value);
                });
                return tests;
            });
    };
}]);

Then inject it in Controller (or wherever) and call it:

testResource.getAll().then(
                        function (data) {
                            $scope.tests = data;
                        }
                    );

You can also implement other methods such as POST, PUT, DELETE.

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.