Angularjs 从中的$http的finally部分调用方法
我想从finally调用一个方法,通过将allProduct函数复制到finally,我的问题将得到解决,但我想调用这个方法Angularjs 从中的$http的finally部分调用方法,angularjs,Angularjs,我想从finally调用一个方法,通过将allProduct函数复制到finally,我的问题将得到解决,但我想调用这个方法 app.controller('productController', function($scope, $http) { $scope.allProduct = function(){ $http({ method: 'post', url: 'ajax.php', data: $.param({'type' :
app.controller('productController', function($scope, $http) {
$scope.allProduct = function(){
$http({
method: 'post',
url: 'ajax.php',
data: $.param({'type' : 'allProduct' }),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data){
$scope.products = data;
})
.error(function() {
$scope.data = "error in fetching data";
});
}
$scope.saveProduct = function(){
var productName = $scope.nProductName;
var productCode = $scope.nProductCode;
var productImage = $scope.nProductImage;
var productCategory = $scope.nProductCategory;
var details = tinymce.activeEditor.getContent();
$http({
method: 'post',
url: 'ajax.php',
data: $.param({'type' : 'saveProduct',
'productName':productName,
'productCode':productCode,
'productImage':productImage,
'productCategory': productCategory,
'productDetails':details}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data){
$scope.products = data;
})
.error(function() {
$scope.data = "error in fetching data";
})
.finally(allProduct);
}});
如何从最终调用allProduct方法 正如JB Nizet所提到的,不推荐使用成功/错误。你应该用then/catch来约束你的承诺。检查$http文档:$http 我会将您的“allProducts”回调声明为一个函数,然后在控制器中需要时引用它,如下所示:
$scope.allProduct = allProductCallback;
$scope.saveProduct = function(){
...
})
.then(function(data){
$scope.products = data;
})
.catch(function() {
$scope.data = "error in fetching data";
})
.finally(allProductCallback);
}});
function allProductCallback(){
$http({
method: 'post',
url: 'ajax.php',
data: $.param({'type' : 'allProduct' }),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.then(function(data){
$scope.products = data;
})
.catch(function() {
$scope.data = "error in fetching data";
});
}
$scope.allProduct。还要注意的是,成功和错误在相当长一段时间内都是不推荐的。使用then()。