我的服务没有更新-Angularjs
My service is not updating - 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
});
但当我从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();
希望它能起作用,
相关文章:
- 基于其他数据结构更新 AngularJS 中的数据结构
- 如何更新 angularjs ng-cell 中的字段
- 使用服务更新 AngularJS 中的控制器
- 如何在用户更新AngularJs中的控制器时刷新$state
- 我的服务没有更新-Angularjs
- 更新angularjs库会导致angular.bootstrap()中出现错误
- 分析异步调用未更新AngularJS中的$scope变量
- 如何通过单击来自javascript外部的复选框来更新AngularJS模型
- 无法更新 angularjs 指令的内容
- 使用 AJAX 更新的模型更新 AngularJS 视图
- Javascript回调方法不更新AngularJS,除非是速记形式
- 是否可以在 iframe 内更新 angularjs 表达式
- 在验证Symfony2中的搜索表单后更新AngularJS范围
- 模型没有't使用所选值更新-AngularJS
- 更新AngularJS中控制器的指令值
- 选择ng选项不使用ajax更新AngularJS中的ng模型
- 在应用程序代码上运行Google Closure编译器后,更新AngularJS模板HTML
- 更新AngularJS中的父子详细信息
- 更新angularjs服务和控制器中的数据
- 使用ng-option更新angularjs