【发布时间】:2017-11-18 08:09:02
【问题描述】:
我正在使用 Pusher for Angular Webapp 进行实时应用程序。当其他会话中的其他人从表单中添加项目时,我需要向数组产品中添加一个新项目。在我的会话中它是有效的。在另一个会话中,如果将数据添加到数组但未显示在 ng-repeat 中,则获取数据。
控制器:
.controller('MainCtrl', ['$scope','Products',function($scope,Products) {
$scope.products = Products.getAll();
var pusher = new Pusher('MY KEY', {
encrypted: true
});
$scope.addProduct = function(product){
Products.addProduct(product);
}
var callback = function(data) {
$scope.products.push(data.NewProduct);
console.log($scope.products);
};
var channel = pusher.subscribe('products');
channel.bind('addProduct', callback);
}]);
查看:
<tbody>
<tr ng-repeat="product in products track by product.id">
<td >{{product.name}}</td>
<td>{{product.unity}}</td>
<td>{{product.price}}</td>
<td>
<button>
Edit
</button>
<button>
Delete
</button>
</td>
</tr>
</tbody>
【问题讨论】:
-
可能是角度摘要循环的问题!。尝试将您的
callback包裹在$evalAsync中 -
感谢@GangadharJannu,但我仍然不明白我该怎么做。
-
见我的answer
标签: angularjs angularjs-ng-repeat pusher