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関数を使って受信メッセージを常にポーリングすることを研究していませんが、役に立つと時間を節約することができます。

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

+0

あなたはChatServiceへの$ scope.messgaepoolデータを送信しますか?それはあなたの必要条件ですか? –

+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); 
}); 

サービスは、それはあなたが感謝:)を助けることを願っています

関連する問題