【发布时间】:2017-10-26 12:15:51
【问题描述】:
我正在通过 Firebase 处理一些数据,并且由于某种原因,当服务器上的值发生更改时,范围在我的控制台中得到更新,但是角度视图没有更新。
我尝试了一些不同的方法,包括将对范围的引用包装在 $timeout 和 $apply 中,但仍然没有解决方案。我曾多次使用 Angular,但以前从未遇到过这种行为。应用中只有一个控制器。
js
app.controller('someCtrl', function($scope, $timeout){
$scope.publishedItems =[];
var fbRef = firebase.database().ref('stories/' + storyId + "/correspondents").orderByChild('published').equalTo(true);
fbRef.on('value', function(snapshot) {
//update scope
$scope.publishedItems = snapshot.val();
console.log($scope.publishedItems)// this logs the correct data, however the markup is not changing
});
html
<div ng-controller="someCtrl">
<div ng-repeat="item in publishedItems">
{{item}}
</div>
</doc>
已解决
所以经过一些调试后,我发现我有一个在 Angular 应用程序之外运行的函数,它向 dom 附加了一些标记,这会干扰 Angulars 的摘要循环。
现在按预期工作
【问题讨论】:
-
你能创建codepen.io 的例子吗?和 $scope.$apply 可能会帮助你
-
@ShirishPatel 我添加了一个 sn-p 虽然我没有公开我的 firebase ref。
-
你可以在publishedItems上使用$watchCollection(),它会开始观察数组的每一个变化。
标签: angularjs firebase firebase-realtime-database