ES6 Promise not Updating AngularJS DOM [duplicate]

Use $q.when to convert ES6 promises to AngularJS promises

AngularJS modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and AngularJS execution context. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc…1 Since the promise comes from outside the AngularJS framework, the framework is unaware of changes to the model and does not update the DOM.

Use $q.when to convert the external promise to an AngularJS framework promise:

function myComponent(){
  ̶P̶r̶o̶m̶i̶s̶e̶.̶r̶e̶s̶o̶l̶v̶e̶(̶)̶.̶t̶h̶e̶n̶(̶_̶ ̶=̶>̶ ̶{̶
  //USE $q.when
  $q.when(Promise.resolve()).then(_ => {
    this.data="Hello World";
  });
}

Use $q Service promises that are properly integrated with the AngularJS framework and its digest cycle.

$q.when

Wraps an object that might be a value or a (3rd party) then-able promise into a $q promise. This is useful when you are dealing with an object that might or might not be a promise, or if the promise comes from a source that can’t be trusted.

AngularJS $q Service API Reference – $q.when

Leave a Comment