0

서버에서 가져온 후 서비스중인 배열에 데이터를 전달해야합니다.컨트롤러에서 서비스로 데이터 전달 서버에서 정보 수신 후

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

controller 바와 같이이 배열이 다음 차례로/도면을보기를 보내는 것을

.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: []을 차지하므로 실제 시간에 뷰가 업데이트됩니다. Ionic Framework 사용. 보너스 : 컨트롤러에서 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); 
}); 

서비스는 당신이 감사합니다 :) 도움이되기를 바랍니다

관련 문제