let loadMapDataPromise = function() {
indexHttpService.getDataPromise().then(function(res) {
$scope.markerArr=res.Data;
});
};
loadMapDataPromise();
console.log($scope.markerArr);
console.log($scope);
为什么在函数外面打印$scope.markerArr是undefined 呢?
但是我在下面试着打印$scope,出来的$scope 上面有markerArr这个数组啊??
你这个跟let
没关系
是因为promise是异步的关系
当你console时,promise的resolve还没执行,所以$scope.markerArr
不存在
只打印$scope
,实际上在console里输出的是一个引用,当你去查看用鼠标点击时才去取值
这时promise已经执行完成了
如果你要打印出$scope.markerArr
,需要在promise的then回调里输出,比如可以这样改
let loadMapDataPromise = function() {
return indexHttpService.getDataPromise().then(function(res) {
$scope.markerArr=res.Data;
});
};
loadMapDataPromise().then(() => { console.log($scope.markerArr); });