0

Guys is it possible to get the $http ajax functions of angularJS without being in a controller?

Currently i have got factories, where i would like to have to database manipulations:

var Services = angular.module('App.Services', []);

Services.factory('Data', function () {

    var data;

    return {
        get:function (id) {
            return merchants[id];
        },
        getAll:function () {
            $http({method: 'GET', url: '/api/merchants'}).
                success(function(data, status, headers, config) {
                    console.log(arguments);
                }).
                error(function(data, status, headers, config) {
                    console.log(arguments);
                });
            return data.slice(0);
        },
        add:function (_data) {
            _data.id = data.length;
            data.push(_data);
        },
        save:function (_data) {
            merchants[_data.id] = _data;
        },
        remove:function (_data) {
            delete merchants[_data.id];
        }
    }

});

How can i use $http now??

1 Answer 1

1

You can use any service within any service. Just inject it like this:

Services.factory('Data', function ($http) {

   // use $http
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.