简体   繁体   中英

How do i array.push from inside PubNub function to Angular scope?

My HTML

ng-app and ng-controller are specified in markup earlier

<div class="statusEntry" ng-repeat="statusInput in statusInputs">
 <span class="userName"> a </span>                
 <span  class="statusMsg"> b </span>                
</div>

Controller

app.controller('globalCtrl', ['$scope', function($scope) {
//someWork
pubnub.subscribe({
channel: "statuses",
callback:
function (data) {

    splitData = data.split(';');
    prepData = '{'+splitData[0]+','+splitData[1]+'}';
    statusInputs.push(prepData);
}
});

When I push the data no new object appears. Angular doesnt get the data, even if i prepend statusInputs with $scope .

It looks like i need to pass the data to root scope from nested functions, i dont know how.

Initialize the array when you first get to your controller:

$scope.statusInputs = [];

and in your callback function change:

statusInputs.push(prepData);

to

$scope.statusInputs.push(prepData);
//also, because pubnub is defined outside of angular
//call $scope.$apply() to force angular to recognize 
//the change on the scope
$scope.$apply()

Basically you are declaring an array on your scope, which your UI will use, and then upon each callback you are appending items into that bound array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM