我使用 Angular JS 从 Firebase 制作了一个显示信息的应用程序。目前,刷新页面后信息会更新,但我想实时更新我的信息。
这是我的角度代码:
var root = angular.module('root',["services", "firebase"]);
root.controller("root4",['$sce', "$scope", function($sce,$scope){
var ref = new Firebase("https://web-quickstart-8326d.firebaseio.com");
var locationRef = rootRef.child('location');
locationRef.once("value", function(snapshot) {
$scope.value = snapshot.val();
$scope.$apply();
if($scope.value=="col6"){
var currentMessageRef = rootRef.child('currentMessage');
currentMessageRef.once("value", function(snapshot) {
$scope.html = snapshot.val();
$scope.trustedHtml = $sce.trustAsHtml($scope.html);
});
} else if($scope.value!="col6"){
$scope.html = "No value in db";
$scope.trustedHtml = $sce.trustAsHtml($scope.html);
}
});
}]);
我在代码中插入了发布信息的条件,但这并不重要......我想要的只是实时更新信息而不重新加载我的页面。
谢谢你的帮助!
功能once
触发一次,然后不再触发。这就是您的信息没有更新的原因。使用on
而是订阅更改。更多信息请点击这里:https://firebase.google.com/docs/database/web/retrieve-data#listen_for_events https://firebase.google.com/docs/database/web/retrieve-data#listen_for_events
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)