Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
Angularjs 阵列推送无法实时工作_Angularjs_Angularjs Ng Repeat_Pusher - Fatal编程技术网

Angularjs 阵列推送无法实时工作

Angularjs 阵列推送无法实时工作,angularjs,angularjs-ng-repeat,pusher,Angularjs,Angularjs Ng Repeat,Pusher,我正在使用Pusher进行Angular Webapp的实时应用。当其他人在其他会话中从表单添加项目时,我需要向数组产品添加一个新项目。在我的课程中,它是有效的。在另一个会话中,如果数据被添加到数组中,但未在ng repeat中显示,则获取该数据 控制器: .controller('MainCtrl', ['$scope','Products',function($scope,Products) { $scope.products = Products.getAll(); var

我正在使用Pusher进行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);
}]);
视图:


{{product.name}
{{product.unity}
{{product.price}}
编辑
删除
稍后在当前范围上执行表达式

所以在代码中向回调函数添加$evalAsync

.controller('MainCtrl', ['$scope','$evalAsync','Products',function($scope,$evalAsync, 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', $evalAsync(callback));
}]);
我是这样做的:

var callback = function(data) {
            $scope.$apply(function(){
                $scope.products.push(data.Producto);
            });
            console.log($scope.products);
        };

可能是角度摘要周期的问题!。尝试用
$evalAsync
包装您的
回调
,谢谢@GangadharJannu,但我仍然不明白我该怎么做。请看我的答案,我是这样做的:var callback=function(data){$scope.$apply(function(){$scope.products.push(data.Producto);});console.log($scope.products); };
var callback = function(data) {
            $scope.$apply(function(){
                $scope.products.push(data.Producto);
            });
            console.log($scope.products);
        };