AngularJS调用$ HTTP不断 [英] AngularJS calls $http constantly

查看:134
本文介绍了AngularJS调用$ HTTP不断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时消息AngularJS很新(4小时新),我试图让一个HTTP调用工作,但好像它发生的事情是什么角保持调用HTTP GET请求了一遍又一遍。我敢肯定,这是因为我的做法是错误的。这就是我想要做的。

Im very new to AngularJS (4 hours new) and I'm trying to get an http call working, however what it seems like its happening is Angular keeps calling the http get request over and over again. I'm sure this is because my approach is wrong. This is what I'm trying to do.

我的控制器文件片段的 web服务工作正常。我在的Node.js应用程序运行此

snippet of my controller file The webservice works fine. I am running this in a node.js app

 function peopleController($scope,$http){
  $scope.getPeople = function(){
    $scope.revar = {};
    $http.get('/location/-79.18925/43.77596').
    success(function(data){
      console.log(data);
      $scope.revar = data;
    });
  }
}

我的list.html文件

My list.html file

   <div ng-controller="busController">
        <div class="blueitem">{{getPeople()}}</div>
   </div>

我知道我会不会看到,因为即时通讯在我的getPeople方法不returing什么结果,但我想看看结果的日志输出我在铬没看,但一万​​次由于角计数保持一遍遍调用该URL方法。相反,它使击球。

I know I will not see the results since im not returing anything in my getPeople Method but I wanted to see the log output of the result which I did see in chrome, but a million times and counting since angular keeps calling that url method over and over again. Instead it keeps hitting.

我要如何角返回响应只有一次?

How do I get angular to return the response just once?

推荐答案

您遇到链接的方式AngularJS作品和问题 - 更precise - 它是如何决定一个模板需要刷新。基本上AngularJS将刷新基于模型的脏检查的模板。不想去考虑太多的细节在这里,因为是一个很好的文章,解释它(的数据绑定angularjs ),但总之它会不断变化的模型的变化,直到它稳定(在模型中没有更多的变化可以看出)。在你的情况下,模型永远不会稳定,因为你与每个调用 getPeople()方法获得新的对象。

The problem you are experiencing is linked to the way AngularJS works and - to be more precise - how it decides that a template needs refreshing. Basically AngularJS will refresh a template based on a dirty-checking of a model. Don't want to go into too much details here as there is an excellent post explaining it (Databinding in angularjs) but in short it will keep changing for model changes till it stabilizes (no more changes in the model can be observed). In your case the model never stabilizes since you are getting new objects with each call to the getPeople() method.

接近,这将是(对可能的解决方案)的正确方法:

The proper way of approaching this would be (on of the possible solutions):

function peopleController($scope,$http){
    $http.get('/location/-79.18925/43.77596').
    success(function(data){
      $scope.people = data;
    });      
} 

然后,在你的模板:

and then, in your template:

   <div ng-controller="busController">
        <div class="blueitem">{{people}}</div>
   </div>

所提到的模板将数据到达时得到自动刷新。

The mentioned template will get automatically refreshed upon data arrival.

再一次,这只是一个可能的解决方案,因此我建议以下AngularJS教程得到了什么是可能更好的感觉: HTTP:/ /docs.angularjs.org/tutorial/

Once again, this is just one possible solution so I would suggest following AngularJS tutorial to get better feeling of what is possible: http://docs.angularjs.org/tutorial/

这篇关于AngularJS调用$ HTTP不断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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