Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJs+;Pubnub数据绑定_Javascript_Angularjs_Pubnub_Ng Bind - Fatal编程技术网

Javascript AngularJs+;Pubnub数据绑定

Javascript AngularJs+;Pubnub数据绑定,javascript,angularjs,pubnub,ng-bind,Javascript,Angularjs,Pubnub,Ng Bind,我使用由提供的PubNub+AngularJS脚本文件。我有一个控制器,可以设置频道并订阅。我在回调函数中设置了scope变量,我看到scope函数中的值正在更新。问题是这个刚刚更新的范围变量没有反映在html页面中。 控制台输出表示pubnub消息调用回调方法,我得到消息。但是由于某些原因,变量数据没有反映到html中。 代码如下: TitleBarController.js $scope.ops_tl = new Object(); $scope.ops_tl.data =

我使用由提供的PubNub+AngularJS脚本文件。我有一个控制器,可以设置频道并订阅。我在回调函数中设置了scope变量,我看到scope函数中的值正在更新。问题是这个刚刚更新的范围变量没有反映在html页面中。 控制台输出表示pubnub消息调用回调方法,我得到消息。但是由于某些原因,变量数据没有反映到html中。 代码如下:

TitleBarController.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);
        }

    });
index.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>

新用户-
{{ops_tl.data.length}

您遇到了与angular中事件循环的处理方式相关的问题。您需要做的是将范围变量的赋值延迟到事件循环上的一个新记号中。要做到这一点,最简单的方法是利用angular中的服务

尝试更改此行:

$scope.ops_tl.data.push(payload.message);
为此:

$timeout(function() {
    $scope.ops_tl.data.push(payload.message);
});

这看起来像是一个有棱角的问题。我认为你需要对
ops\u tl
对象有一些特殊的角度考虑。