1

I want to send the data in my controller to the database via service, but I use a $resource in a service and I don't know how to call it from the controller, below is my controller,

 angular.module('dashboard').controller('DashboardCtrl',['dashboardResource',DashboardCtrl]);

    function DashboardCtrl(dashboardResource){
     var vm = this;

        vm.payments = [
            {
                "paymentId":1,
                "date":"2015/09/13 12.36PM",
                "name":"HSBC Bank",
                "ID": 123456,
                "amount": 1000.00,
                "status": "Active"
            }]
 vm.payments = dashboardResource.get({name: vm.payments.name}, function(payments) {

          payments = vm.payments();
      });
    }

Below is my service

angular.module('lgpsService').factory('dashboardResource',[
        "$resource",dashboardResource]);

    function dashboardResource($resource) {
        return $resource('api/Payment/makePayment');
    }

Please help me to resolve this issue.

3
  • What is the issue? What do you expect to happen, and what happens instead? Commented Sep 24, 2015 at 6:10
  • You need to handle the $promise, but would be helpful to explain your problem more in detail Commented Sep 24, 2015 at 6:13
  • I want to send data to the db but there's an error saying that "GET 0.0.0.0:8000/api/Payment/makePayment 404 (Not Found)" but the path is right. Commented Sep 24, 2015 at 6:14

2 Answers 2

1

I want to send the data in my controller to the database via service

I am not sure if you want to post or get data or here via factory resource. I am going to answer to have both the methods. You can call it via controller in following way and you need to make some changes in service as well.

Your service will be like below

     var app = angular.module('lgpsService').factory('dashboardResource',[
               "$resource",dashboardResource]);

     app.factory('dashboardResource', function($resource) {
             return $resource(
                     'api/Payment/makePayment',
                     {},
                     {
                        'post' {
                             method: 'POST' //To post data
                         },
                         'get' {
                             method: 'GET' //To get data
                          }
                     }
                 );
             });

Your controller

angular.module('dashboard',['lgpsService']).controller('DashboardCtrl',['dashboardResource',DashboardCtrl]);

    function DashboardCtrl(dashboardResource){
     var vm = this;

        vm.payments = [
            {
                "paymentId":1,
                "date":"2015/09/13 12.36PM",
                "name":"HSBC Bank",
                "ID": 123456,
                "amount": 1000.00,
                "status": "Active"
            }]

          //Call service to post data
          dashboardResource.post( vm.payments                                 
                    function(){
                        console.log('data saved successfully');
                    },
                    function(error){
                        AlertService.add( "Error saving data " + error.data);
                        
                    }         
          )
          
          //Call service to get data
         
           dashboardResource.get().
            $then(
                  function(response) {
                    //Do your stuff with data here
                    console.log( response.data );
            },
            function(error) {
                console.log(error);
            }
        );
    }

Hope this helps to you

Sign up to request clarification or add additional context in comments.

Comments

0

First you need to mention ngResource as a dependency in your lgpsService like this

angular.module('lgpsService',['ngResource'])
       .factory('dashboardResource',['$resource',dashboardResource]);

Also you need to angular-resource.js to your HTML page after angular.js.

As your dashboardResource factory is in a different module lgpsService, you need to mention that module as a dependency in your main module like this

angular.module('dashboard',['lgpsService'])
       .controller('DashboardCtrl',['dashboardResource',DashboardCtrl]);

Change your controller function to call save() on your resource object which in turn will make a POST request to that URI.

function DashboardCtrl(dashboardResource){
     var vm = this;

        vm.payments = [
            {
                "paymentId":1,
                "date":"2015/09/13 12.36PM",
                "name":"HSBC Bank",
                "ID": 123456,
                "amount": 1000.00,
                "status": "Active"
            }]
   var payments = dashboardResource.save({name: vm.payments.name}, 
        function() {
             /*success callback. By this time `payments` object is augmented
             with the response from your POST call */
             vm.payments = payments;
      });
}

Another important thing to note is that the return type of any method get/save from $resource is promise object and also it has all the fields copied from the original HTTP response.

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.