2016-11-07 24 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