我的服务没有更新 - Angularjs

时间:2013-12-10 19:32:36

标签: javascript angularjs

我创建了一个service来保存我的所有搜索参数。这样我就可以从我需要的任何地方调用$resource,搜索参数总是正确的。这是service

App.factory('filterService', function () {
  var FilterService = {
    actorId: '0001234',
    actionStatus: ['BUILDING', 'STARTED', 'SENT'],
    payer: null,
    limit: 10,
    offset: 0,
    sortBy: null,
    filterBy: null,
    actionType: ['EB', 'AUTH'],
    actorType: 'REQUESTING'
  };

  return FilterService;
});

因此,当我调用我的REST服务时,我可以访问这样的搜索条件:

App.factory('encounterService', function ($resource, filterService) {
  return {
    resource: $resource('/internal/v2/encounters/:encounterId', {encounterId:'@encounterId'}, {
      search: {
        method: 'GET',
        headers: {
          'RemoteUser': 'jhornsby',
          'Content-Type': 'application/json'
        },
        params: {
          actorType: filterService.actorType,
          actorId: filterService.actorId,
          actionStatus: filterService.actionStatus,
          actionType: filterService.actionType,
          limit: filterService.limit,
          offset: filterService.offset,
          payer: filterService.payer,
          filterBy: filterService.filterBy,
          sortBy: filterService.sortBy
        }
      };
    }
  });

所以我随后打电话给我search

encounterService.resource.search({}, function(data) {
  // do something with data
});

但是当我从filterService更新我的controller时,它不会更新我正在更新的filterService'. Here is an example of the filterService`:

$scope.ok = function() {
    // Access the filter items with this.model
    filterService.actorId = $scope.model.actorId;
    filterService.actionStatus = $scope.model.actionStatus;
    filterService.actionType = $scope.model.actionType;
    filterService.limit = $scope.model.limit;
  }

无论何时我致电search,它始终使用默认值。我怎样才能将搜索参数变成像我试图在这里做的对象?

2 个答案:

答案 0 :(得分:2)

问题是,encounterService中的return语句只在模块初始化时被调用一次。然后评估搜索对象上的params字段,因此filterService在该时间点具有的值是params字段所遵循的值。为了解决这个问题,您需要一种方法来在每次进行查询时评估params对象。一个简单的方法是返回一个可以调用以获取资源的函数,而不是资源本身。

e.g。

App.factory('encounterService', function ($resource, filterService) {
  return {
    getResource: function () {
      return $resource('/internal/v2/encounters/:encounterId', {encounterId:'@encounterId'}, {
        search: {
          method: 'GET',
          headers: {
            'RemoteUser': 'jhornsby',
            'Content-Type': 'application/json'
          },
          params: {
            actorType: filterService.actorType,
            actorId: filterService.actorId,
            actionStatus: filterService.actionStatus,
            actionType: filterService.actionType,
            limit: filterService.limit,
            offset: filterService.offset,
            payer: filterService.payer,
            filterBy: filterService.filterBy,
            sortBy: filterService.sortBy
          }
        }
      };
    }
 }});

然后你可以这样打电话:

encounterService.getResource().search({}, function(data) {
      // do something with data
});

答案 1 :(得分:0)

我会使用filterService像OO对象一样使用getter ans setter。通过这种方式可以很容易地处理您的服务对象并且易于理解代码。

类似的东西:

App.factory('filterService', function () {
  this.FilterService = {
    actorId: '0001234',
    actionStatus: ['BUILDING', 'STARTED', 'SENT'],
    payer: null,
    limit: 10,
    offset: 0,
    sortBy: null,
    filterBy: null,
    actionType: ['EB', 'AUTH'],
    actorType: 'REQUESTING'
  };

   this.getFilterService(){
     return this.FilterService;
   }


  this.setFilterService(service){
    this.FilterService = service;
  }
});

现在从控制器我们可以调用set:

filterService.setFilterService(dummyService);

并致电get:

$scope.theService = filterService.getFilterService();

希望它能起作用,