AngularJS Promises:将promise放在工厂中,这样它就可以全局访问和*编辑*
我试图从外部JSON文件中提取数据,并将其显示给用户查看。通过各种操作,用户可以更改从JSON文件返回的数据,而无需将这些更改写入文件(在本例中,通过单击div将值递增1)。我创建了一个promise服务,它成功地提取并显示数据。我甚至可以得到它,这样数据就可以在单个控制器中更改 这就是我陷入困境的地方:我无法找到对PromiseService中的数据进行任何更改的方法,因此更改无法在全球传播。我如何确保控制器级别的承诺数据的任何更改都将反映在PromiseService中,从而反映在应用程序中的任何数据绑定中?我对承诺不熟悉,所以我对一种完全不同的方法持开放态度 HTML: 按钮控制器(Plunker中的页面控制器):AngularJS Promises:将promise放在工厂中,这样它就可以全局访问和*编辑*,angularjs,promise,factory,Angularjs,Promise,Factory,我试图从外部JSON文件中提取数据,并将其显示给用户查看。通过各种操作,用户可以更改从JSON文件返回的数据,而无需将这些更改写入文件(在本例中,通过单击div将值递增1)。我创建了一个promise服务,它成功地提取并显示数据。我甚至可以得到它,这样数据就可以在单个控制器中更改 这就是我陷入困境的地方:我无法找到对PromiseService中的数据进行任何更改的方法,因此更改无法在全球传播。我如何确保控制器级别的承诺数据的任何更改都将反映在PromiseService中,从而反映在应用程序中
pageApp.controller('buttonCtrl',函数($scope,PromiseService){
$scope.incrementValues=函数()
{
PromiseService.getPromise().then(函数(数据){
$scope.items=数据;
对于(变量i=0;i
incrementValues函数首次成功运行,但每次连续单击都会重新提取承诺并重置数据。总而言之:如何在PromiseService中反映递增的值,而不是局部变量?您的函数每次调用时都会创建一个新的$http调用,因此每次调用时都会返回一个新的承诺,包含新的数据 您每次都需要返回相同的承诺:
var thePromise = $http.get('items.json').then(function(response) {
return response.data;
});
var getPromise = function() {
return thePromise;
};
您可以在工厂中添加一个存储项目的
私有属性。然后创建3种不同的方法来更新和访问该属性
pageApp.factory('PromiseService', function($http) {
var items = {}; // [] in case it is an array
var updateData = function(updatedData){
items = updatedData;
}
var getUpdateData = function(){
return items;
}
var getPromise = function() {
return $http.get('items.json').then(function(response) {
items = response.data;
return response.data;
});
};
return {
getPromise: getPromise,
updateData : updateData,
getUpdateData : getUpdateData
};
});
pageApp.controller('buttonCtrl', function($scope, PromiseService) {
$scope.items = [];
//You should call this method to retrieve the data from the json file
$scope.getData = function(){
PromiseService.getPromise().then(function(data) {
$scope.items = data;
}).catch(function() {
});
}
$scope.incrementValues = function(){
for(var i = 0; i < $scope.items.length; i++){
$scope.items[i]['value']++;
}
PromiseService.updateData($scope.items); //This could be skipped in case you do not want to 'store' these changes.
};
});
将来,您还可以创建一个新方法来更新json本身,而不是内部存储I更新PromiseService工厂以反映您的更改。我不确定它是否应该在控制器中作为承诺或常规对象调用;我两个都试过了,但都不起作用。我现在似乎无法从PromiseService中获得任何数据。你只需要按照我在回答中所说的去做:啊,我在某个地方打错了。感谢您的回复!这非常有效。您想要的是检索数据,一旦完成,就在不同的控制器中更新数据,而不必再次检索数据?@ManuelObregozo-这是正确的。
pageApp.controller('buttonCtrl', function($scope, PromiseService) {
$scope.incrementValues = function()
{
PromiseService.getPromise().then(function(data) {
$scope.items = data;
for(var i = 0; i < data.items.length; i++)
{
data.items[i]['value']++;
}
}).catch(function() {
});
};
});
var thePromise = $http.get('items.json').then(function(response) {
return response.data;
});
var getPromise = function() {
return thePromise;
};
pageApp.factory('PromiseService', function($http) {
var items = {}; // [] in case it is an array
var updateData = function(updatedData){
items = updatedData;
}
var getUpdateData = function(){
return items;
}
var getPromise = function() {
return $http.get('items.json').then(function(response) {
items = response.data;
return response.data;
});
};
return {
getPromise: getPromise,
updateData : updateData,
getUpdateData : getUpdateData
};
});
pageApp.controller('buttonCtrl', function($scope, PromiseService) {
$scope.items = [];
//You should call this method to retrieve the data from the json file
$scope.getData = function(){
PromiseService.getPromise().then(function(data) {
$scope.items = data;
}).catch(function() {
});
}
$scope.incrementValues = function(){
for(var i = 0; i < $scope.items.length; i++){
$scope.items[i]['value']++;
}
PromiseService.updateData($scope.items); //This could be skipped in case you do not want to 'store' these changes.
};
});
$scope.items = PromiService.PromiseService();