1

我想通过服务将控制器中的数据发送到数据库,但是我在服务中使用了 $resource 并且我不知道如何从控制器调用它,下面是我的控制器,

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

请帮我解决这个问题。

4

2 回答 2

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

希望这对你有帮助

于 2015-09-24T06:22:02.217 回答
0

首先,您需要在like this中提及ngResource依赖项lgpsService

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

您还需要angular-resource.jsangular.js.

由于您的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 响应复制的所有字段。

于 2015-09-24T06:27:04.097 回答