3

This is also a general question.

In my example I have to edit the DOM on a Leaflet Map to manipulate the appearance of the legend. We used $timeout as a workaround because the map isn't generated fast enough to get the elements in the map.

So I wanted to know if this is a dirty/messy workaround and if this could be done better than using $timeout?

As suggested some code. The activate function is one of the initiating functions of the map.

controller.js

function activate() {
    projectService.getMarkers(vm, 'projectsData');
    $timeout(function() {
        manipulateLegend();
    });
}
3
  • Generally, yes. You are just trying to force a $digest cycle. Commented Apr 15, 2015 at 12:14
  • 1
    From a quick look, the load event is probably what you want. From it, you have to call Angular and do not forget to call $apply. Commented Apr 15, 2015 at 12:15
  • Try to avoid asking two questions with one stack overflow post - you're not really going to get a meaningful answer for the "general question"(it's way too broad). Your specific question question is hard to answer without actually knowing 1) how you're initializing the map and 2) what's currently calling the activate() function, but you could follow NIkos's suggestions to an answer, probably. Commented Apr 15, 2015 at 12:28

1 Answer 1

1

As far as I understand this is fine, I think the name can lead people into thinking its a bad pattern.

Have read of

http://blogs.microsoft.co.il/choroshin/2014/04/08/angularjs-postdigest-vs-timeout-when-dom-update-is-needed/

and this is interesting

http://lorenzmerdian.blogspot.co.uk/2013/03/how-to-handle-dom-updates-in-angularjs.html

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the articles. I pronounced the question like that because I thought that is not good trying to make async things synchronous and that it in general it is not good blocking the UI.
I am hoping that something like this is going to get more legs github.com/AndrewGaspar/Async-Await-Proposal-for-TypeScript I would so love this, its very awesome in c#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.