I need to pass data to an array in service after getting from server. The controller
runs the function to retrieve data as shown
.controller("messagesController", function($scope, $stateParams, RegisterP) {
// function here retrives data from the RegisterP parameter above calling another service
$scope.messagepool = [ 1, 2]; //I add the data I get here to an array
})
This array then gets sent to a service
.service('ChatService', function() {
return {
chats: [
{
id: "1",
message: "Chat Message 1"
}
],
getChats: function() {
return this.chats;
},
getChat: function(chatId) {
for(i=0;i<this.chats.length;i++){
if(this.chats[i].id == chatId){
return this.chats[i];
}
}
}
}
})
That in turn sends that to a view/views. I need to know how to send the info from controller so its the one occupying the chats: []
so the the views are updated in REAL-TIME. Using the Ionic Framework.
Bonus: I haven't researched having the get function in controllers constantly poll the incoming messages however if you can tell me that it'll be helpful and save time.
controller
.controller("messagesController", function($scope, $stateParams, RegisterP,ChatService) {
// function here retrives data from the RegisterP parameter above calling another service
$scope.messagepool = [ 1, 2]; //I add the data I get here to an array
ChatService.sendData($scope.messagepool);
});
service
.service('ChatService', function() {
return {
sendData:function(data){
this.chatData=data;
console.log(this.chatData);
// this.getChats(); you can call service function from here
},
getChats: function() {
console.log(this.chatData); // it will work here too
return this.chats;
},
getChat: function(chatId) {
for(i=0;i<this.chats.length;i++){
if(this.chats[i].id == chatId){
return this.chats[i];
}
}
}
}
});
Hope it will help you :) Thanks