angularjs承诺不正确解析 [英] angularjs promise not resolving properly

查看:146
本文介绍了angularjs承诺不正确解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器的所有所需的依赖注入。

My controller has all the required dependencies injected.

 $scope.connect = function(url) {
    var defer = $q.defer();
    var promise = $http.get(url).then(function (response) {
         $timeout(function(){
                defer.resolve(response);
            },10000);
        defer.resolve(response);
        $scope.$apply();  //$rootScope.$apply();
    });
    return defer.promise;
 };

$scope.mymethod = function(){
    $scope.globalMydata[];
    console.log("before the http get");
    $scope.connect("MY_URL").then(function(data) {
         console.log("called!", data);
         console.log("INSIDE the http get");
         $scope.mydata = data;
         //$scope.globalMydata.push(data);            
    });
     console.log("after the http get ");
    //do some processing of the returned data here
    var dataHolder = $scope.mydata;
    return calculatedValue;//value from procesing

}

在执行code里面的HTTP GET被调用作为最后的调试日志。我从get调用的结果,但自从后来返回,我无法做任何处理它。这是exactl我们之所以承诺吗?我需要的数据答应做控制器内部进行一些处理。

When the code is executed "INSIDE the http get" is invoked as the last debug log. I get the results from the get call but since its returned later, I am unable to do any processing with it. This is the exactl reason why we promises right? I need the promised data to do some processing inside the controller.

在我的承诺​​实现的任何问题?

Any issue in my promise implementation??

推荐答案

我不知道我是否可以得到你的问题,但看起来你已经建立了一个承诺interseptor,但是从你的问题,它看起来像你只想正规承诺行为。所以我想试试。

I'm not sure whether I get your question, but it looks like you've built a promise interseptor, but from your question it looks like you want just the regular promise behaviour. So I'll try that..

我不是角专家,但我确实使用的$ HTTP承诺了很多,这里就是我如何做到这一点。

I'm not an angular expert but I do use $http promises a lot and here's how I do it.

我注册$ HTTP调用作为一种服务,是这样的:

I register the $http call as a service, like this:

app.service('ajax',function(host,$http){
  this.post = function(api,data,cb){
    $http.post(host + api,data).success(cb)
  };
  this.get = function(api,cb){
    $http.get(host + api).success(cb)
  }
});

主机是predefined 模块。值。我注入这种服务到每一个需要调用HTTP请求,这样操作它们控制器:

host is a predefined module.value. I inject this service into every controller that needs to call http requests and operate them like this:

ajax.post('users', UserToken, function(data){
  console.log('Users points: ' + data.points)
})

据我了解$ HTTP具有内置的诺言,所以没有必要q和defere,这一切都在后台完成。 ajax.post 调用服务,它发送数据主机+'用户',服务器端查找用户通过自己的令牌,并用一个密钥由的名称与用户点的值返回一些数据。客户端:在从它进入,然后控制台登录用户点 CB 功能服务器成功地响应

As I understand $http has promises built in, so there's no need for q and defere, it's all done at the background. ajax.post calls the service, which sends data to host + 'users', server-side looks for user by his token and return some data with one key by the name of points with a value of the users points. Client-side: upon successful reply from the server it proceeds to the cb function which then console logs the user points.

所以我可以做我需要的任何修改,使得Cb函数内部的承诺,因为它不是从服务器的成功答复之前调用。

So I can do whatever modification I need to the promise inside that cb function, since it is not called before a successful reply from the server.

成功错误方法有几个可选的参数,检查出来的这里

The success and error methods have a few more optional arguments, check them out here.

这篇关于angularjs承诺不正确解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆