为什么在使用angularjs的承诺时数据不能被推入数组?

时间:2015-08-07 08:30:08

标签: javascript arrays angularjs

控制者:

var activeArr = [];
var activeDate = [];
var day = (endDate - startDate) / (24 * 60 * 60 * 1000);
for (var i = 1; i < day + 1; i++) {
    activeDate.push(endDate - (24 * 60 * 60 * 1000) * i);

    var start = endDate - (24 * 60 * 60 * 1000) * i;
    var end = endDate - (24 * 60 * 60 * 1000) * (i - 1);

    statisService.getRegStatis(start, end).then(function(response) {
        console.log(response.data.data); //can get data
        activeArr.push(response.data.data)
    });
}
console.log(activeArr); //nothing in the array
console.log(activeDate); // can push data into this array

服务:

userServiceModule.factory('statisService', ['$http','serverUrl', function($http,serverUrl){
    return {
        getRegStatis: function(startDate,endDate){
            var url = serverUrl +"/adminDA/dbReport?startTime="+startDate+"&endTime="+endDate;
            return $http.get(url);
        }
    };
}])

我可以从服务方法中获取数据,之后我想将数据推送到数组中,但是数组中没有任何内容,这是否由承诺引起? 这是控制台: enter image description here

3 个答案:

答案 0 :(得分:3)

首先改变你的工厂:

userServiceModule.factory('statisService', ['$http','serverUrl', function($http,serverUrl){
        return {
            getRegStatis: function(startDate,endDate){
                var url = serverUrl +"/adminDA/dbReport?startTime="+startDate+"&endTime="+endDate;
                return $http.get(url).then(function(result){
                      return result.data;
              });
            }
        };
    }])

然后在控制器中:

var activeArr = [];
var activeDate = [];
var day = (endDate - startDate) / (24 * 60 * 60 * 1000);
for (var i = 1; i < day + 1; i++) {
    activeDate.push(endDate - (24 * 60 * 60 * 1000) * i);

    var start = endDate - (24 * 60 * 60 * 1000) * i;
    var end = endDate - (24 * 60 * 60 * 1000) * (i - 1);

    statisService.getRegStatis(start, end).then(function(data) {
        console.log(data); //can get data
        activeArr.push(data);
        console.log(activeArr); //only now you can log activeArr because you have to wait for promise
    });
}

console.log(activeDate);

$ http确实会返回一个承诺,但您需要使用.then或.success

从工厂函数返回它

答案 1 :(得分:3)

问题是您是在解析promise之前记录数组,更新代码以在更新.then数组后记录callback activeArr内的数组值/ p>

var activeArr = [];
var activeDate = [];
var day = (endDate - startDate) / (24 * 60 * 60 * 1000);
for (var i = 1; i < day + 1; i++) {
    activeDate.push(endDate - (24 * 60 * 60 * 1000) * i);

    var start = endDate - (24 * 60 * 60 * 1000) * i;
    var end = endDate - (24 * 60 * 60 * 1000) * (i - 1);

    statisService.getRegStatis(start, end).then(function(response) {
        console.log(response.data.data); //can get data
        activeArr.push(response.data.data)
        console.log(activeArr); //now your array will be populated
});
}

console.log(activeDate); // can push data into this array

答案 2 :(得分:1)

$ http会返回一个承诺 - 而不是数据。你应该在$ http电话

之后应用一个回调
$http.get('address').success(function(res) {
 //var res is the data sent back from the request..
// in your case it should look like
return res;
});