2016-11-07 12 views
0

下面我將我的粘貼WS連接代碼:角JS手錶不火的時候,數據跺腳改變訂閱

function connect() { 
    var socket = new SockJS('http://localhost:8080/gs-guide-websocket'); 
    stompClient = Stomp.over(socket); 
    stompClient.connect({}, function (frame) { 
     console.log('Connected: ' + frame); 
     stompClient.subscribe('/events/get/1', function (event) { 
      $scope.event = JSON.parse(event.body).path + ": " + JSON.parse(event.body).eventType; 
     }); 
    }); 
} 

和上面這段代碼是內部控制器:

myApp.controller('ConnectionController', ['$scope', function($scope) { 

及以下是我的指令,監聽變化:

myApp.directive('listenerEventsDirective', function() { 
    return{ 
     restrict: 'E', 
     scope: { 
      event: '=' 
     }, 
     link: function(scope, element) { 
      scope.$watch('event', function(newValue, oldValue) { 
       if(newValue){ 
        element.append("<tr><td>" + newValue + "</td></tr>"); 
        alert("newValue " + newValue); 
       } 
      }); 
     } 
    } 
}); 

如果我在控制器申報方法是這樣的:

$scope.changeEvent = function(change){ 
    this.event = change; 
} 

一切正常,但如果我嘗試通過ws訂閱更新我的價值,它將無法正常工作。

即使我更改了stomp訂閱方法中的事件,我該如何使手錶着火?

回答

4

回調被稱爲外角,試着以觸發消化週期來確定改變它包裹在$apply方法:

$scope.$apply(function() { 
    $scope.event = JSON.parse(event.body).path + ": " + 
     JSON.parse(event.body).eventType; 
}); 
+0

謝謝你,現在的工作:) –

+0

歡迎您:) – Karim