how to start angular service (post, get and delete)

I generate a code with editor.swagger and I want to code a GUI with angular for that code. Since I'm newbie in angular, I dind't understand well how to start programming. I first decide to create 3 files : index.html, index.js and service.js.

Here is a part of my code :

I don't know if it's the good beginning or not. But the problem is that I don't have any idea how what I should do for POST and DELETE. Can you help me please?

Answers


You can do the standard 4 methods in Angluar JS(POST,GET,PUT,DELETE). To use these in angular you have to use the $http service that angular provides. To find out more about it check their documentation: https://docs.angularjs.org/api/ng/service/$http


From the Angular $http documentation:

Complete list of shortcut methods:

$http.get
$http.head
$http.post
$http.put
$http.delete
$http.jsonp
$http.patch

You can also use the method parameter in the $http service. So $http(method: 'DELETE', ...)


var app = angular.module('app');

app.service('apiService', ['$http', function($http){

    this.get = function(){
        return $http.get(URL);
    };

    this.delete = function(id){
        return $http.delete(URL, id);
    }

    this.post = function(data){
        return $http.post(URL, data);
    }

}]);

app.controller('controller', ['$scope', 'apiService', function($scope, apiService){

     var getData = apiService.get().success(function(){

     });

}]);

You can use $resource and return it as a service.

app.factory('factoryResource', ['$resource', function($resource) {
var resource = {
    getResource: function(context) {
        var rsc = $resource(context, {}, {'update': {method: 'PUT'}});

        return rsc;
    }
};

return resource; }])

In your controller you should inject the newly created service as below:

app.controller('myCtrl', function($scope, factoryResource) {
    $scope.resource = factoryResource.getResource('http://localhost:8080/api/v1/projects');
    $scope.resource.save(object, successCallback, errorCallback); //post
    $scope.resource.query(successCallback, errorCallback); //get all
    $scope.resource.get({id: object.id}, successCallback, errorCallback); //get a specific object
    $scope.resource.delete({id: object.id}, successCallback, errorCallback); //delete
}

Hope this helps;


Basically we have 4 method for a REST operation

  1. Create (POST)-- To Create a new User (just for an example, it can be anything)
  2. Read (GET) -- To get all users
  3. Update (PUT) -- To update a user's property
  4. Delete (DELETE) -- To delete a user Let me know if you need further clarification.

In angular using service you can do that.

var app = angular.module('app', []);
app.factory('UserService', function($http){
    var user = {};
    user.post = function(data){
        return $http.post('url', data);
    }
    user.get = function(){
        return $http.get('url');
    }
    user.delete = function(id){
        return $http.delete('url');
    }
     user.update = function(id, data){
        return $http.put('url', data);
    }
    return user
});

app.controller('Controller', function('UserService'){
    // Get
    UserService.get().success(function(){

    });
    UserService.post(data).success(function(){

    });
    UserService.delete(id).success(function(){

    });
    UserService.get(id, data).success(function(){

    });

})

This can be done easily through $resource, if you URL is proper rest.


Need Your Help

Trigger a js function to trigger a function in my controller

javascript jquery angularjs scala

Im using scala for my project and I have a controller that have an action func that called:

Qt SDK 1.2.1 on flash drive

c++ qt ide installation usb-flash-drive

Would anyone know how one may go about installing Qt SDK 1.2.1 package on a flash drive for plug and play anywhere? If I wanted to do it for Windows would I simply install it from the installer pa...