尝试在成功后设置超时 - angularjs

时间:2016-07-19 10:12:32

标签: angularjs timeout

我有模态盒子。我想提交表格,显示成功消息,并在3秒后关闭模态框。这是我的ctrl

            $http.post(serviceBase + 'aaaa', data, config)
                .success(function (data, status, headers, config) {
                    $scope.PostDataResponse = data;
                    $scope.messages = ['Success'];

                }, startTimer())
                .error(function (data, status, header, config) {
                    var messages = [];
                    angular.forEach(data.errors, function (value, key) {
                        $scope.ResponseDetails = "Data: " + data +
                                "<hr />status: " + status +
                                "<hr />headers: " + header +
                                "<hr />config: " + config;
                        messages.push('Error! ' + key + ' is not correct');

                    });
                    $scope.messages = messages;
                });
                // expose dependencies on this


};
var startTimer = function () {
    var timer = $timeout(function () {
        $timeout.cancel(timer);
        $location.path('/klupe');
              }, 3000);

};

但是计时器不工作,如果我是console.log $ location.path,没关系,但是函数startTimer()没有调用。日Thnx

1 个答案:

答案 0 :(得分:1)

将函数调用放在成功块内,而不是第二个参数