角$ HTTP调用上的成功$ scope.data没有约束力? [英] Angular $http call not binding on success to $scope.data?

查看:172
本文介绍了角$ HTTP调用上的成功$ scope.data没有约束力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用成功时,有与$ scope.items =数据的问题林。我用这的jsfiddle: http://jsfiddle.net/SAWsA/11/ 但是而不是硬codeD项目我这样做:

Im having an issue with the $scope.items=data when calling success. I am using this jsfiddle : http://jsfiddle.net/SAWsA/11/ However instead of hardcoded items I did this:

   $scope.items=$http({method: 'GET', url: '/test/database/two'}).
   success(function(data, status, headers, config) {
   return data;
   }).
   error(function(data, status, headers, config) {
   $scope.status=status;
   });

和尝试这样做:

$http({method: 'GET', url: '/test/database/two'}).
success(function(data, status, headers, config) {
$scope.items=data;
}).
error(function(data, status, headers, config) {
$scope.status=status;
});

当我把成功函数内的报警我看到lenght为25,所以我知道我得到的数据。然而,当我这个$ HTTP运行后检查$ scope.items,我得到一个未定义后lengh离开我成功的功能。就像它设置本身,失去其设置的范围之内?任何帮助非常AP preciated。

When I put an alert within the success function I see the lenght being 25, so I know i am getting the data. However when I check the $scope.items after this $http run, I get a lengh of undefined after I leave the success function. Like it sets itself and loses it set outside of scope? Any help much appreciated.

推荐答案

$ HTTP 运行时,它会立即返回或者 [] {} IsArray的设置与否>。您传递给成功的功能错误是在稍后的时间执行,当接收数据时。当接收到该数据的 [] {} 您之前将数据进行填充。

When $http runs it will immediately return either [] or {} depending on whether isArray is set or not. The functions you pass to success or error are executed at a later time, when the data is received. When this data is received the [] or {} you had earlier will be populated with the data.

这听起来像你正在运行 $ HTTP 和数据测试它必须从服务器检索的机会了。如果你要使用的数据,那么您的相关code或许应该是成功函数内,直​​到你有你想使用该数据推迟的工作..

It sounds like you are running $http and testing for the data before it has had chance to be retrieved from the server. If you want to use the data then your relevant code should probably be inside the success function to defer the work until you have the data you want to work with..

这篇关于角$ HTTP调用上的成功$ scope.data没有约束力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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