Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$$postDigest and DOM rendering

My understanding of $$postDigest is that it's an opportunity to fire a callback after the current digest cycle has finished - without causing a new digest cycle in the process.

However, does that mean that all DOM effects caused by the current digest cycle are visible to the callback in the $$postDigest.

Given the pseudo code below:

  //some code affecting the width of an element and triggering a digest loop
  myModel.width = 10; //bound back to dom element with id = 'myId'


  $$postDigest(function(){
    //some callback running after the most recent digest loop  
    //gets the width of a DOM element by reading the value directly from the DOM element     
    //that was changed in the previous digest loop
    var width = $('#myId').width();
    //Will width always be 10 here?
  });

Let's assume a bound value in the current digest loop causes the width of a DOM element to change. Then, in $$postDigest I fire a callback with some legacy code that reads the changed width from the DOM element directly. Is it safe to read the value from the DOM element in the $$postDigest since it's supposed to happen after the digest cycle has finished. In other words should the browser be done rendering the effects of the digest cycle by the time the $$postDigest runs?

I am not a fan of this pattern, but have to integrate with some non angular code

like image 856
TGH Avatar asked Nov 09 '22 21:11

TGH


1 Answers

$$postDigest fires a callback function after the current $digest cycle is complete, which means it runs once after all the watches manipulate the DOM and before the browser renders.

like image 111
micmia Avatar answered Nov 14 '22 21:11

micmia