Why call scope.$digest() after $compile(element)(scope) in unit testing

孤街醉人 提交于 2019-12-05 10:29:19

This is a generic code for testing a directive. $Compile binds template with scope and executes link function and $digest/$apply refreshes bindings for models that might have been modified by link.
When you call $compile inside ng-click handler or in controller function, the whole execution is run inside a $digest loop. Angular is built in such a way that dynamically added items (while executing this loop) are executed in same cycle. That is why you don't actually notice the difference and don't realize the need for binding evaluations after compilation. However it's way different in unit tests, where you should tell angular to execute a $digest cycle manually. This often results in problems when testing $q promises, for example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!