如何通过angularjs中的控制器调用服务中的资源

时间:2015-09-24 06:04:48

标签: angularjs angular-resource angular-services angular-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();
      });
    }

以下是我的服务

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

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

请帮我解决此问题。

2 个答案:

答案 0 :(得分:1)

  

我想通过服务

将控制器中的数据发送到数据库

我不确定您是想通过工厂资源发布或获取数据。我将回答这两种方法。您可以通过以下方式通过控制器调用它,您还需要在服务中进行一些更改。

您的服务将如下所示

     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
                          }
                     }
                 );
             });

您的控制器

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);
            }
        );
    }

希望这对你有帮助

答案 1 :(得分:0)

首先,您需要在ngResource中提及lgpsService作为依赖关系

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

此外,您需要angular-resource.jsangular.js到您的HTML页面。

由于您的dashboardResource工厂位于不同的模块lgpsService中,您需要在主模块中将该模块称为依赖

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

更改控制器功能,以便在资源对象上调用save(),然后对该URI发出POST请求。

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;
      });
}

另一个需要注意的重要事项是,从$ resource获取/保存的任何方法的返回类型都是promise对象,并且它还包含从原始HTTP响应中复制的所有字段。