Search code examples
angularjsdata-bindingmodelangular-promise2-way-object-databinding

How to wait till data is assigned to model in Angularjs?


I have a model which I use to iterate and create a tabular structure on my view. I have a button on click of which I have to get the latest value from DB and assign to model. Then I get the innerHTML of model and create a pdf of my current view.

I am facing a issue that before the data is completely assigned to model and view is refreshed the next statement to get the innerHTML of container div executes and I get the older snapshot of my view. How can I hold the execution of my statement until model is done with assignment and view has refreshed?


Solution

  • You should wrap your code in $timeout. This will make sure that your code is executed in the next cycle and you will get the latest snapshot.

    $timeout(function(){
       <your code to get innerHtml>
    })
    

    There is no need to mention any timeout value as this will be called once view has changed

    PS: Add $timeout as dependency.