0

I have this http request, working fine.

Controller

$scope.removeRow = function (od){
  var temp = "order_id=" + od.order_id + "&product_id=" + od.product_id + "&variant_id=" + od.varient_id;
  var req = $http({
      method: 'POST',
      url: 'http://<domain name>/api2/v1/delete_item_in_order',
      data: temp,
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  });

  req.then(
      function (response) {
          alert('success')
      },
      function (error) {
          //$scope.details = response.data;
          alert(error.message);
      }
  ); 
}

Service code to get resource object:

sampleApp.factory('Order', function ($resource) {
   return $resource('http://<domain name>/api2/v1/orders/:id', {id: '@_id'},    {
      'get': {method:'GET'}

    });

});

Question

How to add custom method removeRow in Order service, such that I can use $resource instead of $http in $scope.removeRow() in controller?

1

2 Answers 2

1

Rather than returning a single function, you can return an object with as many methods in following way

sampleApp.factory('Order', function ($resource) { 

    var removeRow = function() {console.log()};
    var getResource = function)() {
        $resource('http://<domain name>/api2/v1/orders/:id', {id: '@_id'}, { 'get': {method:'GET'} }); 
    } 

    return { removeRow : removeRow, 
        getResource : getResource
    } 
});
Sign up to request clarification or add additional context in comments.

Comments

1

There is no need to specify the get method inside $resource, this is already predefined.

Factory:

sampleApp.factory('Order', function ($resource) {
    return $resource('http://<domain name>/api2/v1/orders/:id', {id: '@_id'}, null}).$promise;
});

Calling Method:

$scope.removeRow = function (od){
    var temp = "order_id=" + od.order_id + "&product_id=" + od.product_id + "&variant_id=" + od.varient_id;

    Order.save(temp).then(function(result){
        alert('success');
    }, function(err){
        alert(err.message);
    });
};

Comments

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.