$ http成功范围完成加载可能吗?

时间:2014-05-10 09:26:33

标签: javascript angularjs

$http({
    url: "php/InsertTab.php",
        method: "POST",
        data: {
            'userId': userId,
        },
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function(data, status, headers, config) {

        myVar = data;

        }).error(function(data, status, headers, config) {
    });

如何等待成功范围加载? angularjs的$ http

中没有.​​done

2 个答案:

答案 0 :(得分:2)

您可以使用.success方法:

  

.success(function(data,status,headers,config){

  // this callback will be called asynchronously
  // when the response is available

})

答案 1 :(得分:0)

如果您的意思是更新范围,则可以直接从成功回调中分配范围。

$http({
    url: "php/InsertTab.php",
        method: "POST",
        data: {
            'userId': userId,
        },
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function(data, status, headers, config) {

        $scope.myVar = data;

        }).error(function(data, status, headers, config) {
    });

如果您需要显示加载指示符,则可以使用promise API,它提供then(success, error)方法,如下所示:

function MyCtrl($scope) {
    $scope.ajaxLoading = true;

    $scope.ajaxComplete = function() {
        $scope.ajaxLoading = false;
    };

    $http({
        url: "php/InsertTab.php",
            method: "POST",
            data: {
                'userId': userId,
            },
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).success(function(data, status, headers, config) {

            $scope.myVar = data;

            }).error(function(data, status, headers, config) {
        }).then($scope.ajaxComplete, $scope.ajaxComplete);
}

您可以稍后使用AJAX拦截器进行此通用。请参阅AJAX文档页面:

https://docs.angularjs.org/api/ng/service/ $ HTTP