angularjs - Get contents of child_added while watching firebaseArray -
i have simple private chat, different type of messages, when send server json structure pass message type too.
$scope.messages = firechat.firebasearray; $scope.addmessage = function(e) { //add firebase $scope.messages.$add({ uid: $scope.authdata.uid, text: $scope.msg, timestamp: firebase.servervalue.timestamp, type: 'msg' }); $scope.msg = ""; //reset message };
as can see there property 'type'.
now i'd watch new messages. in documentation there watch api function, returns id
of record/message. i'd contents of message , check type
there , according run other js functions.
$scope.messages.$watch(function (data) { console.log("angularfire watch"); console.log(data); console.log(data.event); console.log(data.key); console.log(data.prevchild); });
above return child_added
-jp6kzwlydtdetz8agpr
, -jp6kzwlydtdetz8ospr
. see, no body under key
there possibility key contents? example
switch (key.type) { case 'paid': animate_paid(); }
edit:
$scope.messages.$watch(function (data) { console.log("angularfire watch"); console.log(data); console.log(data.event); console.log(data.key); console.log($scope.messages.$getrecord(data.key)); });
the above code works finally
you can use $getrecord it:
console.log($scope.messages.$getrecord(data.key));
you can through api arrays here: https://www.firebase.com/docs/web/libraries/angular/guide/synchronized-arrays.html
Comments
Post a Comment