angularjs - 检查 http.get Angular 中执行是否成功

标签 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 中检查?

最佳答案

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

$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
    });
};

关于angularjs - 检查 http.get Angular 中执行是否成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33790035/

相关文章:

java - 使用 AngularJS 和 SPRING 插入数据库

javascript - Jasmine 测试中 Controller 未定义

javascript - 在对象javascript中分配一个键

angularjs - 为什么 Angular $resource 返回一个 Resource 对象而不是类对象?

javascript - 动态指令 : templateUrl

javascript - 如何在 Angular.js 的 ng 类中使用包含破折号的类?

javascript - 如何获取for循环 Angular js中的所有值

javascript - 数据标题包装在 ngTable Angularjs 中

html - 使用 JQLite 重置 CSS 悬停下拉菜单

angularjs - 在单元测试中使用 passThrough 进行 Angular 测试