离子框架实时聊天错误

时间:2016-05-26 23:33:26

标签: javascript angularjs ionic-framework chat

我正在尝试在离子中进行实时聊天,其中消息记录在firebase中但我收到此错误:

Error: $scope.chats.$add is not a function

它假设按组分隔消息的代码,当发送消息时,它会使用$ add将我的聊天发送到firebase 这是我的代码:

angular.module('starter.controllers', ["ionic",'firebase'])

.controller('ChatsCtrl', ['$scope', '$stateParams', '$firebaseArray', '$rootScope', '$ionicHistory', '$state',
  function($scope, $stateParams, $firebaseArray, $rootScope, $ionicHistory, $state) {
           $rootScope.notifyIonicGoingBack = function() {
               $state.go('groups');
            }
            $scope.$myBack = function() {
                $ionicHistory.goBack();
                $rootScope.notifyIonicGoingBack();
              };
            console.log('state ' + $stateParams);
            var ref = new Firebase('https://projectxu.firebaseio.com/chats') //USE OUR Firebase FOR CHAT!!
            $scope.sender_id = 1;
            $scope.group_id = $stateParams.group_id;

            var sync = $firebaseArray(ref);
            sync.$loaded(function (data) {
                var filtered_chats = new Array();
                angular.forEach(data, function(value, key) {
                  if(value.group_id == $scope.group_id) {
                    filtered_chats.push(value);

                 }
                });
                $scope.chats = filtered_chats;
            });




            $scope.sendChat = function(chat){
                console.log("MESSAGE SENT");

              //if($rootScope.authData){
              /*  $scope.chats.$add({
                  user: $rootScope.authData.facebook.username, //SWITCH TO FACEBOOK!!!
                  message: chat.message,
                  imgURL: $rootScope.authData.facebook.cachedUserProfile.profile_img_url
                });
              */

              $scope.chats.$add({
                  sender_id: $scope.sender_id, //SWITCH TO FACEBOOK!!!
                  message: chat.message, 
                  group_id: $scope.group_id, 
                  timestamp: new Date().getTime(),
                  //sender_name: $rootScope.authData.facebook.cachedUserProfile.displayName,
                  //imgURL: $rootScope.authData.facebook.cachedUserProfile.profile_img_url
                });
                chat.message ="";
              console.log("MESSAGE");

            }





}])

0 个答案:

没有答案