AngularJS - why is $apply required to properly resolve a $q promise?

前端 未结 1 1774
难免孤独
难免孤独 2020-12-03 04:44

I\'m trying to write a small service in my angular app which will enable me to pick config parameters specified in global Javascript objects. I don\'t want to attempt access

相关标签:
1条回答
  • 2020-12-03 05:02

    In AngularJS the results of resolve() are propagated asynchronously, inside a $digest cycle, not immediately. This means that callbacks registered with then() will only be called (later) when a digest cycle occurs.

    In your code, nothing is causing Angular to enter a digest cycle, so the then() callback is never called. Calling $apply() is one way to cause a digest cycle to run. Another way: add a button with ng-click that does nothing, then click that, it will cause a digest cycle and you should see the results:

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

    See also https://stackoverflow.com/a/14657974/215945

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题