【问题标题】:How can I update the $scope in AngularJS when a server sided update occurs?当发生服务器端更新时,如何更新 AngularJS 中的 $scope?
【发布时间】:2015-01-01 17:20:46
【问题描述】:

背景

  1. 我正在尝试制作一个使用 PHP、MYSQL 后端和 AngularJS 前端的单页实时应用程序

  2. 我的后端使用的是提供 JSON API 的 CakePHP 框架,而前端使用 AngularJS 中的 ngResource 来访问这个 API。

  3. 我的“Ratchet WebSockets for PHP”正在工作,当后端有更新时,它可以通过 websockets 触发对浏览器的响应。


我的不完整解决方案

解决方案 1: 使用通过 websocket 的响应通过某种 $scope.model.push(websocket.data) 方法来更新 AngularJS 的 $scope 对象?

解决方案 2: 使用通过 websocket 的响应触发 ngResource 上的 Refresh,这将完全刷新 $scope 对象?


问题

我的解决方案可行吗?我怎样才能实现它?

【问题讨论】:

    标签: angularjs cakephp websocket ngresource ratchet


    【解决方案1】:

    您只需要从 WS 读取事件并使用 $emit$broadcast 填充它们,然后您的控制器就可以订阅与它们相关的任何事件。

    看看:

    angular websocket factory

    AngularJS and WebSockets beyond

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      相关资源
      最近更新 更多