如何从firebase检索此类数据级别

时间:2016-05-13 02:02:22

标签: angularjs ionic-framework firebase

Hello All

  

是firebase和ionic的新手,我正在创建一个聊天应用程序   允许我的应用用户能够直接与对方聊天,我有   成功注册用户;获取用户也是   我能告诉每个用户,但我的困惑之处在于我是怎么回事   显示消息

这是我的Firebase结构

Firebase Structure

这是我的观看代码

<ion-view view-title="Home Page">

<ion-content delegate-handle="show-page" >

    <!-- Loading Indicator -->
    <div class="loading"
         ng-if="data.loading" >
        <ion-spinner icon="ripple" ></ion-spinner >
        <p >Loading messages...</p >
    </div >

    <!-- Empty room message -->
    <div class="loading"
         ng-show="!data.loading && data.message.length == 0" >
        <p >This room is empty, be the first to post!</p >
    </div >

    <!-- Discussion list -->
    <div class="chat-messages">
        <ul>
            <li ng-repeat="data in chatMessages">
                {{data.message}}
            </li>
        </ul>
    </div>

</ion-content>

<ion-footer-bar keyboard-attach
                class="bar-assertive item-input-inset" >
    <label class="item-input-wrapper" >
        <input type="text"
               placeholder="Type your message"
               ng-enter="sendMessage()"
               ng-model="data.message" />
    </label >
    <button class="button button-clear"
            ng-click="sendMessage()" >
        Send
    </button >
</ion-footer-bar >

这是我的控制器

app.controller('chatCtrl', function ($scope, $state, $stateParams, $localStorage) {
    $scope.data = {
            messages: [],
            message: '',
            loading: true,
            showInfo: false
    };
    var to                      = $stateParams.UserName;
    $localStorage.from          = $stateParams.MyName;



    var roomName = 'chat_'+(to<$localStorage.from ? to+'_'+$localStorage.from : $localStorage.from+'_'+to);

    console.log(to+', '+$localStorage.from+' => '+ roomName);


    var messagesRef = new Firebase("https://bigzill.firebaseio.com/chatMessages/"+roomName);
    $scope.loadMessages = function () {
        $scope.data.messages    =$firebaseArray(messagesRef);
        $scope.data.messages.$loaded().then(function(data){
            console.log("Angular fire $loaded");
            $scope.data.loading =false;
            $ionicScrollDelegate.$getByHandle('show-page').scrollBottom(true);
        });
    };
    $scope.sendMessage = function(){
            var to               =   $stateParams.UserName;
            $localStorage.from   =   $stateParams.MyName;
            var from             =   $stateParams.MyName;
            var dataToDb         =  {from: from, to: to, message: $scope.data.message,timestamp: new Date().getTime()};
            var roomName = 'chat_'+(to<from ? to+'_'+from : from+'_'+to);
            var InsertChatRef = new Firebase('https://bigzill.firebaseio.com');
            var insertObj   =InsertChatRef.child('chatMessages').child(roomName);
            insertObj.push(dataToDb);

            $scope.data.message = '';

    };
});

1 个答案:

答案 0 :(得分:0)

  哇......编码涉及用锐利敏感的眼睛看。我能够   找出问题。

消息现在正在显示。

  

我所做的是我的loadMessage()函数   $ scope.data.messages,在我看来,我是从一个非常重复的   错误的$ scope,所以我将其更改为ng-repeat =“data.messages中的数据”和   它起作用了