简体   繁体   English

如何在使用AngularJS的工厂中使用“ $ timeout”?

[英]How can I use“$timeout” in factory using AngularJS?

I have the services and within particular time duration if response is come than ok, other wise show error in popup. 如果有响应,我会在特定的持续时间内提供服务,否则会在弹出窗口中显示错误。

Here is my service code: 这是我的服务代码:

angular.module('server', [])

.factory('api', function($http) {
    var server = "http://myapi-nethealth.azurewebsites.net";
        return {
            //Login
            login : function(formdata) {
                return $http({
                    method: 'POST',
                    url: server + '/Users/Login',
                    data: $.param(formdata),
                    headers: { 'Content-Type' : 'application/x-www-form-urlencoded'},
                })
            },
        };

    });

Please tell me how can I use timeout property in services. 请告诉我如何在服务中使用超时属性。

Read this post - How to set a global http timeout in AngularJs 阅读这篇文章- 如何在AngularJs中设置全局http超时

Where you can set a timeout for your http calls. 您可以在其中设置HTTP呼叫超时的位置。

You can inject the above factory in your controller and then make a call with success and error callbacks like below 您可以在控制器中注入上述工厂,然后通过如下所示的成功和错误回调进行调用

api.login(formdata)
.success(function(){ alert("success"); })
.error(function(){ alert("error"); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM