Angular JS: Chaining promises and the digest cycle

前端 未结 1 413
夕颜
夕颜 2020-12-05 14:58

NOTE: the fiddle uses an old version of Angular, and that it\'s not working any more because as of 1.2 the Angular template engine does not handle promises

相关标签:
1条回答
  • 2020-12-05 15:45

    NOTE: the fiddle uses an old version of Angular, and that it's not working any more because as of 1.2 the Angular template engine does not handle promises transparently.

    To quote @pkozlowski.opensource:

    In AngularJS the results of promise resolution are propagated asynchronously, inside a $digest cycle. So, callbacks registered with then() will only be called upon entering a $digest cycle.

    So, when the button is clicked, we are in a digest cycle. then() creates a new promise, but the results of that then() will not be propagated until the next digest cycle, which never comes (because there is no $timeout, or $http, or DOM event to trigger one). If you add another button with ng-click that does nothing, then click that, it will cause a digest cycle and you'll see the results:

    <button ng-click="">Force digest by clicking me</button><br/>
    

    Here's a fiddle that does that.

    The fiddle also uses $timeout instead of setTimeout -- then $apply() isn't needed.

    Hopefully it is clear when you need to use $apply. Sometimes you do need to call it manually.

    0 讨论(0)
提交回复
热议问题