我想从最后调用一个方法,通过复制allProduct函数最终我的问题将被解决,但我想调用这个方法
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方法?
答案 0 :(得分:1)
正如JB Nizet所提到的,成功/错误已被弃用。你应该使用then / catch来链接你的承诺。检查$ http doc:https://docs.angularjs.org/api/ng/service/ $ 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";
});
}