2016-03-08 21 views
0

我有我的app.js這個代碼發送聊天和看我的聊天應用如何採取某些屬性的最後一個值從JSON在Angularjs

$scope.messageshistory = {}; 
$scope.tmp = {}; 

    // send message 
    $scope.sendMessage = function(){ 
     $scope.messages = { 
      from  : $scope.datauser['data']['_id'], 
      fromname : $scope.datauser['data']['nama'], 
      to  : $scope.tmpuserid, 
      message : $scope.tmp['sendmessage'], 
      time  : moment() 
     }; 
      //event emit message 
     socket.emit('message',$scope.messages,function(callback){  
      if(!callback['error']){ 
       $scope.messages['time'] = moment($scope.messages['time']).format('DD-MMMM-YYYY HH:MM'); 


       if ($scope.messageshistory.hasOwnProperty($scope.tmpuserid)){ //yg di json yg paling awal 
        $scope.messageshistory[$scope.tmpuserid].push($scope.messages); 
       }else{ 
        $scope.messageshistory[$scope.tmpuserid] = []; 
        $scope.messageshistory[$scope.tmpuserid].push($scope.messages); 
       } 
       $scope.tmp['sendmessage'] = ''; 
      }else{ 
       var msg = callback['error']; 
       navigator.notification.alert(msg,'','Error Report', 'Ok'); 
      } 

      $scope.$apply(); 
     }); 
    }; 
     //event read message 
    socket.on('message', function (data) { 

     window.plugin.notification.local.add({ 
      id  : moment(), 
      title  : data['fromname'], 
      message : data['message'].substr(0,20) + ' ...', 
      led  : 'A0FF05', 
      json  : JSON.stringify({ routes:'chat', nama :data['fromname'],from:data['from'] }) 
     }); 
     data['time'] = moment(data['time']).format('DD-MMMM-YYYY HH:MM'); 
     if ($scope.messageshistory.hasOwnProperty(data['from'])){ 
      $scope.messageshistory[data['from']].push(data); 
     }else{ 
      $scope.messageshistory[data['from']] = []; 
      $scope.messageshistory[data['from']].push(data); 
     } 

     for(var i = 0; i<= $scope.datauser['data']['friends'].length; i++){ 
      if($scope.datauser['data']['friends'][i]['userid'] == data['from']){ 
       $scope.datauser['data']['friends'][i]['ischat'] = true; 
       break; 
      } 
     }; 
     $scope.$apply(); 
    }); 

我的問題是如何利用最後的消息屬性中的值來自$scope.messageshistory,因爲$scope.messages用於發送消息,$scope.messageshistory用於保留聊天記錄。這是聊天活動圖像:

enter image description here

剛剛從這個活動,$scope.messageshistory將數據保存在它的JSON爲:

{ 
    "5512": [{ 
     "from": "561c", 
     "fromname": "ryan", 
     "to": "5512", 
     "message": "hey", 
     "time": "18-Maret-2016 21:03" 
    }, { 
     "from": "5512", 
     "fromname": "sasa", 
     "to": "561c", 
     "message": "hello", 
     "time": "18-Maret-2016 21:03", 
     "_id": "593s" 
    }] 
} 

我使用angular.toJson($scope.messageshistory)得到這個值,而這個數組如果聊天活動仍在繼續,總是加起來。我的意圖是從$scope.messageshistory獲得消息屬性中的最後一個值,即在我的應用程序中使用Text-to-Speech功能。這是代碼:

$scope.speakText = function() { 

    TTS.speak({ 
      text: **this the place for the code**, 
      locale: 'en-GB', 
      rate: 0.75 
     }, function() { 
      // handle the success case 
     }, function (reason) { 
      // Handle the error case 
     }); 
    }; 

它將讀取$scope.messageshistory中的最後一條消息。那麼,我必須編寫哪些代碼才能獲取最後一個值?

+0

看起來像'$ scope.messageshistory'是一個對象。您想獲取特定userId密鑰的最新消息?或者由套接字發出的最新消息? '$ scope.messageshistory [$ scope.tmpuserid] [$ scope.messageshistory [$ scope.tmpuserid] .length-1]''怎麼辦? – cl3m

+0

@ cl3m等待我會試試 –

+0

@ cl3m它的工作,但它仍然是所有的財產,如何把'消息'屬性 –

回答

0

你必須做到以下幾點:

var msgs = $scope.messageshistory[$scope.tmpuserid] 
var yourLastMessage = msgs[msgs.length-1].message 
// you could also add a quick check so you don't get 
// an error if the messages array is emtpy : 
// var yourLastMessage = (msgs && msgs[msgs.length-1] ? msgs[msgs.length-1].message : null) 

編輯

按您的評論的一些解釋:

var msgs = $scope.messageshistory[$scope.tmpuserid] 
// msgs is now an Array containing Objects 
// [{message : 'xxx'},{message : 'yyy'}] 
// we take the last entry of the msgs Array (msgs.length-1) 
// so msgs[msgs.length-1] is the last object ({message : 'yyy'}) 
// and finally we take the 'message' property' of that object: 
var yourLastMessage = msgs[msgs.length-1].message 
+0

它的工作就像一個魅力,感謝您的答案。最後一個問題,所以我寫'var yourLastMessage =(msgs&amp; msgs [length-1]?msgs [msgs.length-1] .message:null)'或者只是'var yourLastMessage = msgs [msgs.length-1 ] .message'? –

+0

因爲你總是在你的數組中推入一個值('$ scope.messageshistory [$ scope.tmpuserid] .push($ scope.messages);'),所以你可以合理地跳過測試 – cl3m

+0

你可以看看我以前的題? http://stackoverflow.com/questions/35811385/how-do-make-hardware-back-button-become-exit-app如果你不介意它。在那個問題中,我想讓後退按鈕硬件在用戶進入'home'頁面時變成退出應用程序。謝謝 –

0

假設歷史記錄對象的鍵升數,並考慮到並非由W3C指定的鍵的對象的順序,你必須做到以下幾點:

  1. 得到所有鑰匙
  2. 找到 「最新」(因此最大數量)
  3. 把它拿來

,所以你可以例如做

var keys = Object.keys($scope.messagehistory); 
keys.sort (function (a, b) { 
    return a - b; 
}); 
var result = keys[keys.length - 1]; 
相關問題