我的服务没有更新-Angularjs

My service is not updating - Angularjs

本文关键字:更新 -Angularjs 服务 我的      更新时间:2024-06-02

我创建了一个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
});

但当我从controller更新filterService时,它不会更新我的filterService'. Here is an example of the过滤器服务"正在更新:"

$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时,它总是使用默认值。如何将我的搜索参数变成一个对象,就像我在这里尝试做的那样?

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

例如

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

我会使用类似filterService的OO对象和getter和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;
  }
});

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

filterService.setFilterService(dummyService);

并呼叫获取:

$scope.theService = filterService.getFilterService();

希望它能起作用,