0

Мне нужно передать данные в массив в службе после получения с сервера. controller запускает функцию для извлечения данных, как показаноПередача данных в службу от контроллера ПОСЛЕ получения информации с сервера

.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 
}) 

Этот массив отправляется на службу

.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]; 
     } 
    } 
    } 
} 
}) 

Это, в свою очередь посылает его на вид/виды. Мне нужно знать, как отправлять информацию с контроллера, поэтому она занимает chats: [], поэтому представления обновляются в режиме реального времени. Использование ионной структуры. Бонус: Я не исследовал, что функция get в контроллерах постоянно проверяет входящие сообщения, однако, если вы можете сказать мне, что это будет полезно и сэкономить время.

+0

Вы хотите отправить данные $ scope.messgaepool в ChatService? Это ваше требование? –

+0

Да, вот что я хочу. – Olli

ответ

0

контроллер

.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('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]; 
     } 
     } 
    } 
    } 
}); 

Надеется, что это поможет вам :) Спасибо