[英]How do i array.push from inside PubNub function to Angular scope?
我的HTML
ng-app
和ng-controller
在前面的標記中指定
<div class="statusEntry" ng-repeat="statusInput in statusInputs">
<span class="userName"> a </span>
<span class="statusMsg"> b </span>
</div>
調節器
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);
}
});
當我推送數據時,沒有新對象出現。 即使我在statusInputs
加上$scope
,Angular也不會獲取數據。
看來我需要將數據從嵌套函數傳遞到根作用域,我不知道如何。
首次訪問控制器時,初始化數組:
$scope.statusInputs = [];
並在您的回調函數中更改:
statusInputs.push(prepData);
至
$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()
基本上,您是在您的作用域上聲明一個數組,您的UI將使用該數組,然后在每次回調時將項目追加到該綁定數組中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.