我有一个控制器使用 StompJS 订阅一个 url(后端是 Spring Java),它每 5 秒返回一个交替的字符串“list”和“box”。我想在 StompJS 收到一些数据时更新我的 UI 元素,但我无法更新 UI 元素。我已经用 a 测试了相同的逻辑,$timeout
并且 UI 正在更新,因此它必须与回调函数的工作方式有关。谁能看到 UI 没有更新的原因是什么?
我有这些简单的 UI 元素:
<input ng-model="ctrl.uniqueId"/>
<input ng-model="test"/>
ctrl.uniqueId
是验证实际控制器实例是否正在更新。出于某种原因,每次只有 1 个控制器进行 5 次不同的订阅。如果有人可以提供帮助,那也很棒,但我怀疑除非您看到我所有的代码设置,否则您能否获得很多信息。
无论如何,在我的控制器中(尝试了 self.test 但它没有用,所以我尝试使用 $scope.test 来查看它是否有所作为):
self.uniqueId = window.performance.now();
$scope.test = 'list';
// the UI will be updated to dummy after 3 seconds.
$timeout(function() {
$scope.test="dummy";
}, 3000);
// the UI will not update.
var callBackFn = function(progress) {
$scope.test = progress;
console.log(self.uniqueId + ": " + $scope.test);
};
// the server returns alternating data (list and box) every 5 seconds
MyService.subscribeForUpdate('/topic/progressResults', callBackFn);
如果重要的话,这是我的 StompJS 服务代码:
self.subscribeForUpdate = function(channelUrl, callBackFn) {
self.socket.stomp.connect({}, function() {
self.socket.subscription = self.socket.stomp.subscribe(channelUrl,
function (result) {
//return angular.fromJson(result.body);
callBackFn(result.body);
return result.body;
}
);
});
};
这是console.log
结果:
1831.255000026431: list
1831.255000026431: box
额外:是否可以在没有类似于 Promise 的回调函数的情况下获取返回数据?