javascript - $scope.$apply() 是做什么的?

标签 javascript angularjs

当我在我的 Angular 应用程序中通过 websockets 接收数据时,我一直在使用 $scope.$apply() 来更新我的模型的绑定(bind)并且它有效。但它实际上做了什么,为什么需要调用它来实现更新?

最佳答案

如果您调用 $apply,所提供的代码将在 angular-context 中执行,您可以使用 angular 提供的内容。

来自link :

Angular modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and Angular execution context. Only operations which are applied in Angular execution context will benefit from Angular data-binding, exception handling, property watching, etc...

enter image description here

You can also use $apply() to enter Angular execution context from JavaScript. Keep in mind that in most places (controllers, services) $apply has already been called for you by the directive which is handling the event. An explicit call to $apply is needed only when implementing custom event callbacks, or when working with third-party library callbacks.

关于javascript - $scope.$apply() 是做什么的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18710478/

相关文章:

javascript - 如何使用 Jquery AJAX 提交多个表单

javascript - express JS : serving files out of memory vs serving static files

javascript - 未知提供者 : $animateCssProvider <- $animateCss <- $uibModalStack <- $uibModal

javascript - 当我不知道属性名称时,如何从 javascript 对象获取值

node.js - 在带有 windows 主机的 ubuntu 虚拟服务器上运行 Node、Yeoman、Grunt 等

javascript - 通过键将元素添加到数组时出现奇怪的行为

javascript - jQuery 从 Alt 添加图像标题属性

java - 在 Java 中解析 JavaScript 代码以检索变量的名称

javascript - 从 ngResource 查询中的 PUT 请求中删除正文

javascript - 根据下拉列表更新 ng-repeat 中的值