当我在我的 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...
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/