【问题标题】:How to send multiple objects from factory to REST service如何将多个对象从工厂发送到 REST 服务
【发布时间】:2015-11-19 14:20:12
【问题描述】:

我是 AngularJS 的新手,在将多个对象从我的工厂发送到我的 REST 服务时遇到了麻烦。

我已经成功完成了一个使用过滤器对象调用 RESTful 服务的函数,

myFactory.getActivities =  function(filter) {
    return $http.post('ws/activities', filter);
};

但现在我需要修改 REST 服务以接收更多对象“分页”。

我试过了:

myFactory.getActivities = function (filter, pagination) {
    return $http.post('ws/getActivities', {
        activityId : $scope.activityId,
        otherParameter : $scope.otherParameter
    },{
        numPage : $scope.numPage,
        numRegisters : $scope.numRegisters
  });
};

但它不起作用(因为 $scope 没有在工厂中定义)。

我的问题是,我应该怎么做才能将这些值发送到我的 REST 服务?

【问题讨论】:

    标签: angularjs rest factory


    【解决方案1】:

    只有控制器和指令有 $scope,你必须将它作为参数传递:

    myFactory.getActivities = function (filter, pagination, scope){...

    或者,更清楚的是,只传递你想要的值:

    myFactory.getActivities = function (filter, pagination, otherParameter, numPage,numRegisters){...

    或更短:

    myFactory.getActivities = function (filter, pagination, objectWithData){...

    【讨论】:

      【解决方案2】:

      一种简洁的方式来设计您的工厂或服务:

      Factory.js

      angular.module('myApp')
        .factory('myFactory', function($http, $q) {
          return ({
            getActivities: getActivities
          });
      
         // Restful POST call with 4 parameters
         // @Param: activityId, otherParameter, numPage, numRegisters
         function getActivities (activityId, otherParameter, numPage, numRegisters) {
           var request = $http({
             method: 'post',
             url: 'ws/activities',
             params: {
               activityId: activityId,
               otherParameter: otherParameter,
               numPage: numPage,
               numRegisters: numRegisters
             }
           });
           return( request.then ( handleSuccess, handleError) );
         }
      
         // When POST success, it returns stauts code
         // should be status: 200
         function handleSuccess( response ) {
           return (response.status);
         }
         // When fail to POST data, if return the failure message
         function handleError( response ) {
           return($q.reject(response.data.message));
         }
        });
      

      Controller.js

      angular.module('myApp')
        .controller('MainController', function($scope, myFactory) {
          [...]
          myFactory.getActivities($scope.activityId, $scope.otherParameter, $scope.numPage, 
                                  $scope.numRegisters).then(
            function(status) {
              // you get the status from backend
              // do whatever you want here
            },
            function(error) {
             // fail to POST to backend
             // implement your logic how to handle failure
            }
          );   
      });
      

      【讨论】:

      • 这很好,谢谢。如果我需要该工厂中的其他功能,我应该添加 return ({ getActivities: getActivities, otherFunction : otherFunction });到代码?
      • @jnoo 请将其标记为答案,它对社区有帮助。一般来说,是的,如果你需要其他功能。但是handleSuccesshandleError 函数可能有不同的实现。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      相关资源
      最近更新 更多