2017-03-17 6 views
0

私はIonicアプリでチャットをしましたが、ユーザーが新しいメッセージを受信して​​チャット中に表示されてもメッセージは表示されません。手動でスクロールして表示する必要があります。データが追加されたときにページの一番下までスクロールするにはどうすればよいですか?

新しいメッセージが表示されたら、ページの下部に自動的にスクロールできますか?

<ion-view view-title="Chat Detail"> 


    <ion-content class="padding" start-y="430"> 
     <!-- *messages/content messages --> 
     <ol class="messages"> 

      <!-- *self/container self messages --> 
      <li ng-repeat="message in messages" ng-class="{self: message.userId === myId, other: message.userId !== myId}"> 
       <!-- *avatar/avatar image --> 
       <div class="avatar"> 
        <img ng-src="{{ message.profilePic }}"> 
       </div> 
       <!-- *msg/messages --> 
       <div id="data" class="msg"> 
        <p>{{ message.text }}</p> 
        <time> 
         <i class="icon ion-ios-clock-outline"></i> 
         {{message.time}} 
        </time> 
       </div> 
      </li> 
     </ol> 
    </ion-content> 
    <!-- *bar-detail/color bar and input --> 
    <ion-footer-bar keyboard-attach class="item-input-inset bar-detail"> 
     <label class="item-input-wrapper"> 
     <input onblur="this.focus()" autofocus type="text" placeholder="Type your message" on-return="sendMessage(); closeKeyboard()" ng-model="data.message" on-focus="inputUp()" on-blur="inputDown()" /> 
     </label> 
     <a class="button button-icon icon ion-arrow-return-left" ng-click="sendMessage()" ></a> 
    </ion-footer-bar> 
</ion-view> 

EDIT:コントローラが追加しました:/

使用http://ionicframework.com/docs/api/service/ $ ionicScrollDelegate:

//Recieve messages 
     $timeout(function(){ 
     var ref = firebase.database().ref('/messages/' + $scope.currentDiscussion); 
     ref.orderByChild("timestamp").on("value", function(snapshot1) { 

      $timeout(function(){ 
      $scope.messages = snapshot1.val(); 

      }) 
      }) 

     }) 
     window.scrollTo(0,document.body.scrollHeight); 

     $scope.sendMessage = function() { 

      if ($scope.blocked === 1){ 

      // An alert dialog 

    var alertPopup = $ionicPopup.alert({ 
     title: 'BLOCKED', 
     template: 'This discussion is blocked.' 
    }); 

    alertPopup.then(function(res) { 

    }); 
    } else { 

      var d = new Date(); 
      var g = new Date(); 
      d = d.toLocaleTimeString().replace(/:\d+ /, ' '); 
      g = g.toLocaleTimeString().replace(/:\d+ /, ' ') + new Date(); 



      $scope.sendMessages = firebase.database().ref('/messages/' + $scope.currentDiscussion).push({ 
      userId: myId, 
      text: $scope.data.message, 
      time: d, 
      timestamp: g, 
      profilePic: $scope.displayProfilePic, 

      }); 

      firebase.database().ref('/accounts/' + myId + "/discussions/" + $scope.currentDiscussion).update({ 
      lastMessage: $scope.data.message, 
      time: d, 
      blocked: 0, 
      blockedOther: 0, 
      }); 

      if ($scope.newMessages === ""){ 
      firebase.database().ref('/accounts/' + $scope.savedUid + "/discussions/" + $scope.currentDiscussion).update({ 
       lastMessage: $scope.data.message, 
       name: $scope.displayName, 
       profilePic: $scope.displayProfilePic, 
       time: d, 
       discussionId: $scope.currentDiscussion, 
       blocked: 0, 
       newMessages: 1, 
       userId: myId, 
       }); 
      } else { 
       firebase.database().ref('/accounts/' + $scope.savedUid + "/discussions/" + $scope.currentDiscussion).update({ 
       lastMessage: $scope.data.message, 
       name: $scope.displayName, 
       profilePic: $scope.displayProfilePic, 
       time: d, 
       discussionId: $scope.currentDiscussion, 
       blocked: 0, 
       newMessages: $scope.newMessages + 1, 
       userId: myId, 
       }); 
      } 

       firebase.database().ref('/accounts/' + $scope.savedUid).update({ 

       messagesTotal: $scope.messagesTotal + 1, 
       }); 


      delete $scope.data.message; 
      $ionicScrollDelegate.scrollBottom(true); 
     } 

     }; 

     $scope.inputUp = function() { 
      if (isIOS) $scope.data.keyboardHeight = 216; 
      $timeout(function() { 
      $ionicScrollDelegate.scrollBottom(true); 
      }, 300); 

     }; 

     $scope.inputDown = function() { 
      if (isIOS) $scope.data.keyboardHeight = 0; 
      $ionicScrollDelegate.resize(); 
     }; 

     $scope.closeKeyboard = function() { 
      // cordova.plugins.Keyboard.close(); 
     }; 

     }) 
     $scope.leaveChat = function() { 
      var userId = firebase.auth().currentUser.uid; 
      firebase.database().ref('accounts/' + userId).update({ 
      currentDiscussion: "", 
      }) 



     }; 


     $scope.data = {}; 
     $scope.myId = myId; 

     $scope.messages = []; 

答えて

0

はあなたがここに$ionicScrollDelegateサービスを確認することができ、ここで

は、私のコードのHTMLです方法

$ionicScrollDelegate.scrollBottom([shouldAnimate]) 

shouldAnimateは、アニメーションscroolを必要とするかどうかを指定するブール型です。

+0

こんにちは、それは私がメッセージを送信するときに使用しているものですが、自動的にスクロールしますが、メッセージを受け取ったときにこれはうまくいかず、それをどのように統合するのか分かりません。あなたのアイデアがあるかどうかを調べるためにコントローラのコードを追加しましたか?事前にありがとうございます – FrenchyNYC

+0

角度の '$ watch'サービスを使って' $ scope.messages'オブジェクトのリスナを作成することができます。リスナでは、 '$ scope.messages'がその値を変更するたびに' $ ionicScrollDelegate.scrollBottom(]) 'を呼び出します。 – mQuixaba

+0

実際のコードに統合するために私の初期コードを更新してもよろしいですか?私はこの解決策を試みているかなり頭痛を抱えています(私はかなり新しいJavaScriptです)。 – FrenchyNYC

関連する問題