javascript - $http.post -> start (用于预加载器)

标签 javascript jquery ajax angularjs http

在我的 Controller 中,我现在有:

$http.post('/api/agendainsert', { appointment_datetime: appointment_date + appointment_hour, profile: profile, column_id: column_id })
            .success(function(data, status, headers, config){
                $state.go('form.success');
            })
            .error(function(data, status, headers, config) {
                console.log("Data: " + data +
                    "<hr />status: " + status +
                    "<hr />headers: " + headers +
                    "<hr />config: " + config);
        });
    };

$http.postSTARTS时,我可以使用什么功能吗?因此,我可以在屏幕上显示预加载器,并在成功提取数据后隐藏它。

最佳答案

您将需要一个服务来执行此操作。考虑创建一个 WaitService 。 WaitService 将监听两个事件 wait:startwait:stop .

何时 wait:start你必须使用指令在 <body> 上注入(inject)加载动画。当wait:stop那么你必须删除那个加载动画。您可以在 $rootScope 中观看这些事件.

现在包括这个WaitService在您的应用程序中并像这样使用。

$scope.$emit('wait:start'); //indicate start loading
$http.post('/api/agendainsert', { appointment_datetime: appointment_date + appointment_hour, profile: profile, column_id: column_id })
            .success(function(data, status, headers, config){
                $scope.$emit('wait:stop'); //indicate remove loading
                $state.go('form.success');
            })
            .error(function(data, status, headers, config) {
                $scope.$emit('wait:stop'); //indicate remove loading
                console.log("Data: " + data +
                    "<hr />status: " + status +
                    "<hr />headers: " + headers +
                    "<hr />config: " + config);
        });
    }; 

关于javascript - $http.post -> start (用于预加载器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32776769/

相关文章:

javascript - 使用 React 动态渲染组件

javascript - Tumblr 风格的 UserHoverCard

javascript - 有什么方法可以快速轻松地将整个网站的标准网站加载转换为 AJAX?

jquery - $(this) 在函数中不起作用

javascript - 使用 AngularJS 使用 Angular Material 按 Enter 键提交表单

javascript - JavaScript 中的命名空间技术,推荐吗?性能?需要注意的问题?

javascript - .js 文件和 jQuery AJAX 方法

javascript - 如何使用jquery在跨域iframe中更改css 跨域意味着外部链接

javascript - 拖放并更改dom中div的父级

javascript - 如何将通过 ajax 加载的元素附加到之前通过 ajax 加载的另一个元素?