Answer the question
In order to leave comments, you need to log in
How to expand an array in AngularJS?
I found the extend method in the documentation, but it does not work as I want with arrays, an example is here jsfiddle.net/Lvc0u55v/11448
https://docs.angularjs.org/api/ng/function/angular...
Tried to do concat() , but probably not suitable for my task. I am doing a chat. The first time the chat page is loaded, I get the chat history as an array and save it to a local array in the service using $rootScope.apply() so that the data dynamically flies to the controller. The problem is that the data in the controller is not automatically updated when new messages are received. If you use angular.extend to expand the array, then the display problem is solved, but the problem arises that angular.extend simply overwrites one array with another, and does not connect as desired.
UPDI will give the code from my chat
Service, problematic place
function chatService($rootScope, flashService) {
var host = location.origin.replace(/^http/, 'ws');
var socket = new WebSocket(host);
var initialization = {
token: localStorage.getItem("satellizer_token"),
getHistory: true,
index: $rootScope.chatIndex //index defined first time on server
}
socket.onopen = function(obj) {
socket.send(JSON.stringify(initialization));
};
var self = this;
self.live = [];
self.error = false;
socket.onmessage = function(obj) {
var response = JSON.parse(obj.data);
if (!response.error) {
$rootScope.chatIndex = response.index;
angular.forEach(response.data, function(value, key) {
response.data[key].date = response.data[key].date.substring(11, 19);
});
// if single msg
if (response.data.length == 1) {
$rootScope.$apply(function() {
self.live.push(response.data[0]);
// console.log('single message from server', response.data[0]);
});
}
//if get history from server
else {
$rootScope.$apply(function() {
//!!! ПРОБЛЕМА ЗДЕСЬ !!!
angular.extend(self.live, response.data); // в контроллере новое сообщение видно, но стрирается старое в self.lve, мне надо сохранить старое и дописать туда новое ,т.е. было ['1'], пришло с сервера ['2','3'], в контроллер отдали ['1', '2', '3']
// self.live = self.live.concat(response.data); // в self.live нормально сохраняется новое сообщение, но в контроллер оно не попадает
});
}
} else {
$rootScope.$apply(function() {
self.error = true;
flashService.error(response.errorMessage, false);
});
}
}
self.msgSend = function(msg) {
if (socket.readyState == 1) {
socket.send(JSON.stringify(msg));
}
}
self.getHistory = function() {
initialization.index = $rootScope.chatIndex;
initialization.getHistory = true;
socket.send(JSON.stringify(initialization));
}
}
function chatController($scope, chatService, flashService, $rootScope, $location) {
$scope.msgSend = function() {
if (!$scope.isError()) {
flashService.clearFlashMessage();
obj.text = $scope.textMsg;
obj.date = moment().format("YYYY-MM-DD HH:mm:ss");
obj.token = localStorage.getItem("satellizer_token");
chatService.msgSend(obj);
$scope.textMsg = '';
}
};
$scope.getHistory = function() {
chatService.getHistory();
}
//!!! ПРОБЛЕМА ЗДЕСЬ !!!
// new messages
$scope.live = chatService.live;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question