检查是否在http.get angular中执行成功

时间:2015-11-18 20:47:02

标签: angularjs

$scope.func1 = function() {
    $http.get(url, {params}).success(function(result){
        // code
    }).error(function(error){
        // error
    })
}

$scope.func2 = function() {
    $scope.func1();
    // when http of func1 done executing, call following code
}

如果func1的http.get成功执行,如何检查func2?

1 个答案:

答案 0 :(得分:5)

通过正确使用承诺,您可以链接多个承诺:

$scope.func1 = function () {
    return $http.get(url, {/*params*/})
        .then(function (response) { // success is deprecated, use then instead
            // code
            return something;
        })
        .catch(function (error) {   // use catch instead of error
            // error
        });
};


$scope.func2 = function () {
    $scope.func1().then(function(something) {
        //when http of func1 done executing, call following code
    });
};