如何更新服务angularJS中的控制器变量
这是我的控制器如何更新服务angularJS中的控制器变量,angularjs,Angularjs,这是我的控制器 sampleApp.controller('SupplierController', ['$scope', '$http', 'SupplierService', function ($scope, $http, SupplierService){ $scope.Suppliers = []; $scope.getSuppliers = function() { SupplierService.getSuppliers().then(funct
sampleApp.controller('SupplierController', ['$scope', '$http', 'SupplierService', function ($scope, $http, SupplierService){
$scope.Suppliers = [];
$scope.getSuppliers = function() {
SupplierService.getSuppliers().then(function(data) {
$scope.Suppliers = data;
});
};
$scope.editSupplier = function(supplier) {
SupplierService.editSupplier(supplier);
editMode = false;
};
$scope.getSuppliers();
}]);
这是我的服务
sampleApp.factory('SupplierService', function($http, $q) {
var SupplierService = {};
var SupplierList = [];
SupplierService.getSuppliers = function() {
var Info = {};
Info.Action = "GET";
Info = JSON.stringify (Info);
var req = {
url: SupplierURL,
method: 'POST',
headers: { 'Content-Type': 'application/json'},
data: Info
};
if ( SupplierList.length === 0 )
{
return $http(req).then(function (response) {
SupplierList = response.data
return response.data;
});
}
else
{
var deferred = $q.defer();
deferred.resolve(SupplierList);
return deferred.promise;
}
};
SupplierService.addNewSupplier = function(supplier) {
var Info = {};
Info.Action = "ADD";
Info.SupplierName = supplier.name;
Info.SupplierMobile = supplier.mobile;
Info = JSON.stringify (Info);
var req = {
url: SupplierURL,
method: 'POST',
headers: { 'Content-Type': 'application/json'},
data: Info
};
$http(req)
.success(function(data) {
alert ('Supplier update is successful.');
})
.error(function (data, status, headers, config) {
alert ('Supplier update error.');
});
};
SupplierService.editSupplier = function(supplier) {
var Info = {};
Info.Action = "UPDATE";
Info.SupplierID = supplier.id;
Info.SupplierName = supplier.name;
Info.SupplierMobile = supplier.mobile;
Info = JSON.stringify (Info);
var req = {
url: SupplierURL,
method: 'POST',
headers: { 'Content-Type': 'application/json'},
data: Info
};
$http(req)
.success(function(data) {
alert ('Supplier update is successful.');
})
.error(function (data, status, headers, config) {
alert ('Supplier update error.');
});
};
return SupplierService;
});
我想要的是,只有当editSupplier的http调用成功时,我才希望执行这一行
editMode = false;
当前,上述行位于$scope.editSupplier函数中。因此,无论成功或失败,它都会被召唤。如何将此线路移动到服务
任何更好的方法都将受到高度赞赏。根据您当前的设置,最简单的方法是返还$httpreq,因为这是一个承诺。因此,在文章的结尾,供应商会说:
return $http(req);
在控制器中,您可以执行以下操作:
SupplierService.editSupplier(supplier).success(function(response) {
editMode = false;
});
如果您在发生错误时有特定的事情要做,您还可以链接.error处理程序。根据您当前的设置,最简单的方法是返回$httpreq,因为这是一个承诺。因此,在文章的结尾,供应商会说:
return $http(req);
在控制器中,您可以执行以下操作:
SupplierService.editSupplier(supplier).success(function(response) {
editMode = false;
});
如果您在发生错误时有特定的事情要做,您可以另外链接.error处理程序。@tandrewnichols窃取了我的初始答案,因此我将提供另一种选择。:p 将editMode移动到服务中,并使用getter检查editMode的值。
另外,您是否打算在应用程序中使editMode成为全局变量?@tandrewnichols窃取了我的初始答案,因此我将提供另一种选择。:p 将editMode移动到服务中,并使用getter检查editMode的值。 另外,您是否打算在应用程序中使editMode成为全局变量