1

나는이 튜토리얼 http://g00glen00b.be/spring-websockets-angular/ 아래의 stomp-websockets, sock.js와 함께 angular.js를 사용하고 있습니다. websocket에서 메시지를 받고 있지만 websocket의 메시지가 도착한 후에 템플릿보기가 새로 고쳐지지 않습니다. 템플릿보기를 업데이트하려면 입력 상자를 지우거나 마우스로 스크롤해야합니다.템플릿보기가 업데이트되지 않았습니다 -> 무언가 스크롤이 필요하거나 입력을 지울 필요가 없습니다

$scope.initiator = false; 
$scope.socket = { 
    client: null, 
    stomp: null 
}; 

$scope.initSockets = function() { 
    $scope.socket.client = new SockJS('/myUrl'); 
    $scope.socket.stomp = Stomp.over($scope.socket.client); 

    $scope.notifyMessage = function(message) { 
     $scope.initiator = true; 
     $scope.messages.push(message) 
    }; 

    $scope.socket.stomp.connect({}, function() {   
     $scope.socket.stomp.subscribe("/topic/messages", $scope.notifyMessage);      
    });  

    $scope.socket.client.onclose = $scope.reconnect;  
}; 

$scope.initSockets(); 


template view: 
<ul><li ng-repeat="item in messages track by $index">{{item}}</li></ul> 

답변

1

범위 적용하려면 $ apply를 사용해야 할 수도 있습니다. afaics sock.js는 각도를 만들지 않으므로 범위에 변화가 있음을 각도에 알려야합니다. 범위를 사용하여이 작업을 수행합니다. $ apply.

$scope.notifyMessage = function(message) { 
    $scope.$apply(function(){ 
     $scope.initiator = true; 
     $scope.messages.push(message) 
    }); 
};