Angularjs 角形JS手表赢得';t在stomp subscribe中更改数据时激发
下面我将粘贴我的ws-connection代码:Angularjs 角形JS手表赢得';t在stomp subscribe中更改数据时激发,angularjs,websocket,Angularjs,Websocket,下面我将粘贴我的ws-connection代码: function connect() { var socket = new SockJS('http://localhost:8080/gs-guide-websocket'); stompClient = Stomp.over(socket); stompClient.connect({}, function (frame) { console.log('Connected: ' + frame);
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) {
$scope.changeEvent = function(change){
this.event = change;
}
下面是我关于倾听变革的指示:
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);
}
});
}
}
});
一切都在运行,但如果我试图通过ws-subscribtion更新我的值,它将不起作用
即使我在stomp subscribe方法中更改事件,如何使watch激发?您的回调在angular外部调用,请尝试将其包装在
$apply
方法中,以触发摘要循环以确定更改:
$scope.$apply(function () {
$scope.event = JSON.parse(event.body).path + ": " +
JSON.parse(event.body).eventType;
});
您的回调在angular外部调用,请尝试将其包装在
$apply
方法中,以触发摘要循环来确定更改:
$scope.$apply(function () {
$scope.event = JSON.parse(event.body).path + ": " +
JSON.parse(event.body).eventType;
});