AngularJS为什么此控制器变量未在UI中更新?

问题描述 投票:0回答:1

我有一个使用StompJS的控制器来订阅一个url(后端是Spring Java),该URL每5秒返回一个交替的字符串“ list”和“ box”。我想在StompJS收到一些数据时更新UI元素,但是无法获取UI元素进行更新。我已经使用$ setTimeout测试了相同的逻辑,并且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的返回数据?

我有一个使用StompJS的控制器来订阅一个url(后端是Spring Java),该URL每5秒返回一个交替的字符串“ list”和“ box”。当StompJS ...

angularjs callback model-binding stompjs
1个回答
0
投票

这是一个非常常见的问题,当与angularjs一起使用第三方库(不在角度环境中时,会发生这种情况。在这种情况下,您需要使用以下命令手动触发摘要周期:

© www.soinside.com 2019 - 2024. All rights reserved.