Currently, I am developing an Angular application integrated with Firebase, and one of the features I'm working on is a one-to-one chat system. My goal is to check if a chat room already exists between the user using the application and the person they want to chat with by querying Firebase. If the room exists, I intend to assign that specific room as the selected room within the scope. To achieve this, I am utilizing the "Messages" service to execute the query.
this.roomQuery = function(user1ID, user2ID) {
roomsRef.orderByChild("user1").equalTo(user1ID).on("child_added", function(snapshot) {
if (snapshot.val().user2 == user2ID) {
self.selectedRoom = snapshot.key();
console.log(self.selectedRoom);
} else {
self.selectedRoom = null;
}
})
}
In my controller, I have incorporated the following:
$scope.$watch(
function(){ return Messages.selectedRoom },
function(newValue, oldValue) {
$scope.selectedRoom = newValue;
}
)
Typically, the $scope.$watch
method has been effective in updating values, yet it occasionally fails in this scenario. Although the console consistently displays the correct value for Messages.selectedRoom, there are instances where $scope.selectedRoom does not reflect the update. This discrepancy leaves me puzzled. Shouldn't the log appearing correctly imply that the scope has also been updated?