Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Firebase child_已删除,无法实时工作_Angularjs_Firebase - Fatal编程技术网

Angularjs Firebase child_已删除,无法实时工作

Angularjs Firebase child_已删除,无法实时工作,angularjs,firebase,Angularjs,Firebase,我正在使用Angularjs和Firebase跟踪tutsplus实时web应用程序。 我在下面有main.js,它允许我在Firebase中实时添加和更改项目,而无需在Chrome和Safari中刷新浏览器。 但是,当我从Firebase中删除邮件时,我必须刷新浏览器以更新邮件列表-因此不是实时的。我看不出问题出在哪里 /*global Firebase*/ 'use strict'; /** * @ngdoc function * @name firebaseProjectApp.co

我正在使用Angularjs和Firebase跟踪tutsplus实时web应用程序。 我在下面有main.js,它允许我在Firebase中实时添加和更改项目,而无需在Chrome和Safari中刷新浏览器。 但是,当我从Firebase中删除邮件时,我必须刷新浏览器以更新邮件列表-因此不是实时的。我看不出问题出在哪里

/*global Firebase*/
'use strict';

/**
 * @ngdoc function
 * @name firebaseProjectApp.controller:MainCtrl
 * @description
 * # MainCtrl
 * Controller of the firebaseProjectApp
 */
angular.module('firebaseProjectApp')
  .controller('MainCtrl', function ($scope, $timeout) {
    var rootRef = new Firebase('https://popping-inferno-9738.firebaseio.com/');
    var messagesRef = rootRef.child('messages');

    $scope.currentUser=null;
    $scope.currentText=null;
    $scope.messages=[];

    messagesRef.on('child_added', function(snapshot){
        $timeout(function() {
            var snapshotVal = snapshot.val();
            console.log(snapshotVal);
            $scope.messages.push({
                text: snapshotVal.text,
                user: snapshotVal.user,
                name: snapshot.key()

            });         
        });
    });

    messagesRef.on('child_changed', function(snapshot){
        $timeout(function() {
            var snapshotVal = snapshot.val();
            var message = findMessageByName(snapshot.key());
            message.text = snapshotVal.text;
        });
    });
    messagesRef.on('child_removed', function(snapshot){
        $timeout(function() {
            var snapshotVal = snapshot.val();
            var message = findMessageByName(snapshot.key());
            message.text = snapshotVal.text;
        });
    });
    function deleteMessageByName(name){

        for(var i=0; i < $scope.messages.length; i++){
            var currentMessage = $scope.messages[i];
            if(currentMessage.name === name){
                $scope.messages.splice(i, 1);
                break;
            }
        }
    }
    function findMessageByName(name){

        var messageFound = null;
        for(var i=0; i < $scope.messages.length; i++){
            var currentMessage = $scope.messages[i];
            if(currentMessage.name === name){
                messageFound = currentMessage;
                break;
            }
        }
        return messageFound;
    }

    $scope.sendMessage = function(){

        var newMessage = {
            user: $scope.currentUser,
            text: $scope.currentText 
        };

        messagesRef.push(newMessage);
    };


  });

从Firebase中删除消息时调用的代码:

messagesRef.on('child_removed', function(snapshot){
    $timeout(function() {
        var snapshotVal = snapshot.val();
        var message = findMessageByName(snapshot.key());
        message.text = snapshotVal.text;
    });
});
这段代码实际上从未从HTML/DOM中删除消息

有一个方便的deleteMessageByName方法来处理删除。因此,如果您将上述内容修改为此,它将起作用:

messagesRef.on('child_removed', function(snapshot){
    $timeout(function() {
        deleteMessageByName(snapshot.key());
    });
});

你已经调试过了吗?例如:程序是否接收到一个child_removed事件?回答如下。请注意,我花了半个多小时的时间在上复制了您的问题。对于下一个问题,您可以自己设置这样的复制,或者最好将问题中的代码量减少到显示问题所需的最小值。再见,谢谢。不知何故,我没有看到deleteMessageByName方法实际上没有被调用。我会听从你的劝告。