简体   繁体   English

Angular js在返回之前等待数据

[英]Angular js wait for data before return

I have an angular service where I make an http post call. 我有一个有角度的服务,我打了一个http post电话。 Also, some constant values are set there. 此外,还设置了一些常量值。 In the end I use return to send these data to the controller, do some logic and display them in a view. 最后我使用return将这些数据发送到控制器,做一些逻辑并在视图中显示它们。

The service code: 服务代码:

var apiurl = 'myurl';
  var apidata = {data: 'mydata'};
  var myDataPromise = httppostrequest($http, apiurl, apidata);

    myDataPromise.then(function(result) {
        service.datas = result;
        console.log("data.name"+result);
    });
    return service;

The httppostrequest function: httppostrequest函数:

function httppostrequest($http, apiurl, apidata){
    return $http({
        method : "POST",
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        url: apiurl,
        data: JSON.stringify(apidata),
        timeout: 5000
    });
}

The request is successful and returns the data. 请求成功并返回数据。 The problem is that the line 问题是这条线

return service; 退货服务;

is executed before the data from the request are assigned to the service variable. 在将来自请求的数据分配给服务变量之前执行。 Therefore the data from the post request are not even passed to the controller. 因此,来自post请求的数据甚至不会传递给控制器​​。

I have worked with node js, where there are ways and tools to avoid the asynchronous nature when needed. 我使用过节点js,其中有方法和工具可以在需要时避免异步性质。 With angularjs I don't get it. 使用angularjs我不明白。

You described perfectly what is happening. 你完美地描述了发生了什么。 So my suggestion is to init the value of datas inside the service to an empty value (object, array, or whatever it will be after fetched the promise), associate the promise to the service, and simply move the promise resolution where you really need to manipulate that data. 所以我的建议是将服务中的datas值初始化为空值(对象,数组或者在获取承诺之后的任何内容),将promise与服务相关联,并简单地将promise解析移动到您真正需要的位置操纵那些数据。 So inside your service: 所以在你的服务中:

var apiurl = 'myurl';
var apidata = {data: 'mydata'};
service.datas = []; // if it will be an array for example
service.myDataPromise = httppostrequest($http, apiurl, apidata);

return service;

And then wherever you need it, just inject your service and do: 然后在您需要的任何地方,只需注入您的服务并执行:

myService.myDataPromise.then(function(result) {
  myService.datas = result;
  console.log("data.name"+result);
});

Whatever return you have you will have to put it inside the success method. 无论您有什么回报,您都必须将其置于成功方法之内。 Otherwise you will see that issue. 否则你会看到这个问题。

You can do something like this: 你可以这样做:

 var data = {} //your data
 var promiseURLMetaData = HttpService.httpPost("parseUrlMetadata", data);
 promiseURLMetaData.then(function (response) {
     var urlMetaData = response.data;
     return urlMetaData;
  })
 .catch(function (error) {
    console.log("Something went terribly wrong while trying to get URL Meta Data.");
 });

Return your service inside the result function. 在结果函数中返回您的服务。

var apiurl = 'myurl';
var apidata = {data: 'mydata'};
var myDataPromise = httppostrequest($http, apiurl, apidata);

    myDataPromise.then(function(result) {
        service.datas = result;
        console.log("data.name"+result);
        return service;
    });

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

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