doutan2228 2016-10-15 20:11
浏览 43

为什么我的Angularjs范围对象为空? [重复]

This question already has an answer here:

My controller has the following code:

app.controller("weeklyLogViewer", function ($scope, $http){
$scope.allLogs = {};

$http({
    method: 'POST',
    url: '../Utilities/WeeklyLog.php',
    data: $scope.dateSelected,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function successCallback(response)
{
    $scope.allLogs = response.data;
});

console.log($scope.allLogs);});

But when I write $scope.allLogs to console it shows empty object but when I do console.log($scope); and view the allLogs array I can see the data.

The response.data is:

[
{"id" : 001, "name" : "name", "age" : 20},
{"id" : 002, "name" : "name", "age" : 21},
{"id" : 003, "name" : "name", "age" : 22},
{"id" : 004, "name" : "name", "age" : 23}
]

What am I doing wrong?

</div>
  • 写回答

2条回答 默认 最新

  • doupa9062 2016-10-15 20:14
    关注

    Because console.log() is executing before the value is assigned in the $scope.allLogs object. try to log inside the then(); This is happening due to asynchronous behaviour of $http which is an ajax call internally;

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)