Angularjs仅刷新angular.element

时间:2016-01-13 17:20:58

标签: angularjs

我使用angularjs,我的网站每秒都从websocket接收数据,现在每次我得到一个事实我都会使用$scope.$apply进行刷新,问题是当我的服务器发送比平时更多的信息时页面非常慢,如果我评论$scope.$apply网络正常运行。

我有类似的东西

$scope.ChangeData=function(str){
  //process.... take around 1ms
  //here i change a variable, with new info

  $scope.$apply();
 //here the process print 60-100ms
}

然后我想这样做,

 $scope.ChangeData=function(str){
          //process.... take around 1ms
          //here i change a variable, with new info 
angular.element(document.getElementById(str.ID)).scope().$apply

         //I do this, I hope the process will only take a few ms
         //beacuse i change only the changed element  
        }

但我在这部分中有错误

  

无法阅读财产' $ apply'未定义的

请帮帮我..

更新 Example in plnkr

1 个答案:

答案 0 :(得分:-1)

如果不完全理解应用程序的上下文,我只能猜测这是一个消息传递类型的应用程序。这个假设基于以下事实:a)您正在使用套接字服务器将更新推送到UI上的现有数据; b)您正在尝试维护已存在的现有UI。

我带着你的傻瓜把它分叉,我将如何处理这个问题。有两件事需要实现:

  • ItemsController被愚弄,因此它只消耗来自服务的模型以及模拟推送以更新到所述模型
  • itemsService便利
    • 存储消息数据
    • 处理推送更新

由于ng-repeat已经收取正在观看其目标集合中的任何更改,因此您不必执行任何$scope.$applyangular.element你正在做的事情。 ng-repeat中的每个项目都会为您更新DOM。这就是它的目的。

https://plnkr.co/edit/qLeDuP0iaTUE16rY2KTl?p=preview