角度$ scope.$ watch具有可返回承诺的函数 [英] angular $scope.$watch with an function that returns a promise

查看:98
本文介绍了角度$ scope.$ watch具有可返回承诺的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对异步函数的返回值进行监视,但仍会遇到无限摘要循环.这是我的$ watch

  $ scope.$ watch(function(){var friendsCount = PublicUserData().then(function(data){console.log(data.length);返回data.length;});返回friendsCount;},函数(newValue,oldValue){console.log("change" + newValue);}); 

我在chrome控制台中看到两个 console.log 都被调用,但是第二个(在回调函数上)被调用的频率比第一个要高得多.

PublicUserData 使用 $ q :

  .factory('PublicUserData',function($ http,$ q){返回函数(){var defer = $ q.defer();$ http.get('/api/v1/users/').then(function(data){defer.resolve(data.data.users);})返回defer.promise;}}) 

我已经尝试了一些事情,例如将watch表达式设置为我的 $ watch 之外的$ scope,但是最终返回了工厂的代码,而不是工厂的返回值./p>

是否可以将 $ watch 与实现诺言的功能一起使用?还是我应该使用其他东西?

解决方案

您真的真的不想接受来自 $ watch 的AJAX请求.真的.

Angular使用脏检查,并且每次 $ scope 上的任何内容更改时,都会重复调用 $ watch 函数.如果您可以使它正常工作,则在向服务器发送垃圾邮件时会完全破坏应用程序的性能.

您似乎想要做的事情是拥有一个计数器或显示已登录多少朋友的东西?通过服务器事件或轮询机制可以更好地解决该问题,轮询是最容易实现的.

请考虑以下类似内容:

  $ interval(function(){PublicUserData().then(function(data){$ scope.data.friendsCount = data.length;});},1000); 

这将每秒轮询一次服务器(这可能是过大的,但比尝试在 $ watch 中执行此操作的频率要低得多),并使用以下命令更新 $ scope 正确的值.

I am trying to setup a watch on the return value of an asynchronous function, but keep running into infinite digest loops. Here's my $watch

  $scope.$watch(function(){
    var friendsCount = PublicUserData().then(function(data){
      console.log(data.length);
      return data.length;
    });
    return friendsCount;
  }, function(newValue, oldValue) {
    console.log("change"+ newValue);
  });

I can see in the chrome console that both console.log get called, but the second (on the callback) gets called far more often than the first.

PublicUserData uses $q:

.factory('PublicUserData', function($http, $q){
  return function(){
    var defer = $q.defer();
    $http.get('/api/v1/users/').then(function(data){
      defer.resolve(data.data.users);
    })
    return defer.promise;
  }
})

I've tried a few things, like setting the watch expression as a $scope outside of my $watch, but that ends up returning the factory's code, rather than the factory's return value.

Is it possible to use $watch with a function that implements a promise? Or should I be using something else?

解决方案

You really really really really don't want to be doing AJAX requests from a $watch. Really.

Angular uses dirty-checking and your $watch function will be invoked repeatedly every single time anything on the $scope changes. If you could get it to work it would completely kill the performance of your application while spamming the server.

It looks like what you're trying to do is to have a counter or something that shows how many friends are logged in? That problem is better solved with either server events or a polling mechanism, polling being the easiest to pull off.

Consider something like this instead:

$interval(function() {
    PublicUserData().then(function(data) {
        $scope.data.friendsCount = data.length;
    });
}, 1000);

That will poll the server every second (which is probably overkill, but far less frequent than trying to do it in a $watch) and update the $scope with the correct value.

这篇关于角度$ scope.$ watch具有可返回承诺的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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