当范围变量AngularJS更新摘要不会被触发 [英] Digest is not triggered when scope variable is updated in AngularJS

查看:165
本文介绍了当范围变量AngularJS更新摘要不会被触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AngularJS和angularFire展现我的任务列表:

I am using AngularJS and angularFire to show a list of my tasks:

<ul ng-repeat="tool in tools">
    <li>{{tool.name}} {{ tool.description}}</li>
</ul>

var toolRef = new Firebase(dbRef + "/tools/" + toolId);
toolRef.once('value', function(snapshot) {
      console.log(angular.toJson(snapshot.val()));
      $scope.tools.push(snapshot.val());
      //$scope.$apply();
});

http://jsfiddle.net/oburakevych/5n9mj/11/

code是非常简单的:我绑定一个'现场'对象到我的火力地堡DB。所述对象包含的相关工具的ID的列表。然后,我加载在$ scope.tools变量和使用NG重复每一个工具,以显示他们在UI。
但是,每次我推一个新的进入我的$ scope.tools - 不更新DOM。我要调用$ $范围适用于触发消化后,每推 - 见注释掉线18,然后它的作品

Code is very simple: I bind a 'site' object to my Firebase DB. The object contains a list of ID of relevant tools. Then I load every tool in the $scope.tools variable and use ng-repeat to show them in the UI. However, every time I push a new entry into my $scope.tools - the DOM is not updated. I have to call $scope.$apply to trigger digest after every push - see commented out line 18 and then it works.

因为我与angularFire约束范围变量现在,只有播下这几次这真是奇怪。

It's really strange since I sow this several times now and only with scope variables bound with angularFire.

有人能解释一下吗?难道我做错了什么?

Can anyone explain this? Am I doing anything wrong?

推荐答案

由于您更改AngularJS以外的范围,你必须使用$范围。$适用()通知角左右范围的变化。这是处理的常用方法。要使用AngularJS的错误处理,我会敷code在回调函数,如:

Because you change the scope outside of AngularJS, you must use $scope.$apply() to inform Angular about the scope changes. That is a common way to handle that. To use the error handling of AngularJS i would wrap the code in a callback function like:

$scope.$apply(function(){
  $scope.tools.push(snapshot.val());
});

这篇关于当范围变量AngularJS更新摘要不会被触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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