我使用 Pubnub 提供的PubNub +AngularJS 脚本文件。我有一个控制器来设置频道并订阅它。我在回调函数中设置了作用域变量,我看到作用域函数中的值正在更新。问题是这个刚刚更新的范围变量没有反映在 html 页面中。控制台输出说 pubnub 消息调用回调方法,我得到消息。但是由于某种原因,变量数据没有反映到 html 中。这是代码:
标题栏控制器.js
$scope.ops_tl = new Object();
$scope.ops_tl.data = new Array();
$scope.ops_tl.data.push("dummy");
console.log("pp ", $scope.ops_tl==undefined);
PubNub.ngSubscribe({ channel: channelsToAutoSubscribe[0] });
$rootScope.$on(PubNub.ngMsgEv(channelsToAutoSubscribe[0]), function(event, payload) {
// payload contains message, channel, env...
console.log('got a message event:', payload);
if(payload.channel == channelsToAutoSubscribe[0]) {
// i.e. ops_tl channel
// parse message and populate channel specific variable
console.log($scope.ops_tl.data);
$scope.ops_tl.data.push(payload.message);
console.log($scope.ops_tl.data);
}
else {
console.log("Received message %s from an unknown channel %s", message, channel);
}
});
索引.html
<div class="btn-group" ng-controller="TitleBarController">
<button data-toggle="dropdown" class="btn dropdown-toggle">
New users -
<span> {{ ops_tl.data.length }} </span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li ng-repeat="item in ops_tl.data"><a href="#/onboarding/{{ item.data.id }}">New user - {{ item.data.name }}</a></li>
</ul>
</div>