0

I need to load some data into my angular app the first time it is loaded. The data needs to come from the server.

My app currently consists of a number of directives on a single view in an accordion. The directives are forms whose criteria needs to be met before the user can progress to the next accordion group. The first accordion group needs to use the data from the server to display to the user, there is a dependency on the data for the view.

What is the best way to inject this data in the first instance?

I could move the initial data call into the first directive but it doesn't really feel like it is 'its' job. I could create another directive to handle this load but then I would need manage the load order of the directives. I have had a look at ng-init which works well until the server call is made, during this call the directives run before it completes.

Any of the above would work for me in theory but i'm not sure that they are the best approach. What would you suggest is the best approach?

Thanks

4
  • are you using any router in your app? Which? Commented Dec 9, 2015 at 10:11
  • what scope are you using in your directives? Commented Dec 9, 2015 at 10:12
  • @hitmands $routeProvider -ngRoute for routing Commented Dec 9, 2015 at 10:20
  • @TarunDugar Isolated Scope for directives Commented Dec 9, 2015 at 10:21

2 Answers 2

1

You should use a resolve in your state, so, both views and controllers are called when the resolution is done.

have a look at this link: http://odetocode.com/blogs/scott/archive/2014/05/20/using-resolve-in-angularjs-routes.aspx

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

Comments

0

You can decorate your directives with ng-if attribute and, as value, pass a scope property that will indicate data load ex.

Comments

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.