Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我的服务没有更新-Angularjs_Javascript_Angularjs - Fatal编程技术网

Javascript 我的服务没有更新-Angularjs

Javascript 我的服务没有更新-Angularjs,javascript,angularjs,Javascript,Angularjs,我已经创建了一个服务来保存我的所有搜索参数。这样我就可以在任何需要的地方调用我的$resource,搜索参数总是正确的。这是服务 App.factory('filterService', function () { var FilterService = { actorId: '0001234', actionStatus: ['BUILDING', 'STARTED', 'SENT'], payer: null, limit: 10, offset:

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

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;
});
因此,当我调用my 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
        }
      };
    }
  });
encounterService.resource.search({}, function(data) {
  // do something with data
});
encounterService.getResource().search({}, function(data) {
      // do something with data
});
然后我调用我的
search
,如下所示:

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
        }
      };
    }
  });
encounterService.resource.search({}, function(data) {
  // do something with data
});
encounterService.getResource().search({}, function(data) {
      // do something with data
});
但是当我从
控制器
更新我的
过滤器服务
时,它不会更新我的
过滤器服务。以下是正在更新的
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
,它总是使用默认值。如何将我的搜索参数转换为我在这里尝试执行的对象?

问题是,当初始化模块时,
遇到服务
中的return语句只调用一次。然后对搜索对象上的
params
字段进行求值,因此
filterService
在该时间点上的值就是
params
字段所使用的值。为了避免这种情况,您需要一种方法,在每次进行查询时计算params对象。一种简单的方法是返回一个函数,您可以调用该函数来获取资源,而不是资源本身

e、 g

你可以这样称呼它:

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
        }
      };
    }
  });
encounterService.resource.search({}, function(data) {
  // do something with data
});
encounterService.getResource().search({}, function(data) {
      // do something with data
});

我会像使用getter和setter的OO对象一样使用
filterService
。通过这种方式,将很容易处理您的服务对象,也很容易理解之后的代码

比如:

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

希望它能起作用,

从控制器更新filterService的代码在哪里?添加了更新代码我从来没有这样做过。你能给我举个例子吗?我已经举过了。也许“结束”这个词把你甩了。在编写Javascript时,您需要随时创建闭包,您只需要知道它。尝试