Join the Stack Overflow Community
Stack Overflow is a community of 6.9 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am developing one directive for my grid control, and i need to get some common resource data from server before i build the grid control.

How i can write initialization function for the directive? I need it to execute before loading the control to DOM and it has to be something like lazy one(it should execute only when the DOM got directive).

I have written one function in the controller of directive to get the resource from server, but the directive execution is not waiting for the server response. it is just continuing the execution and throwing resource value is undefined.

Please anyone help to me solve this?

share|improve this question

Either you could use the resolve function of your state to have the data loaded before the controller is instantiated at all, see https://docs.angularjs.org/api/ngRoute/provider/$routeProvider

Or you could do it the angular way and

  • initialize an empty array for your data in the controller
  • make the ajax request in the controller, and update the array when the response comes in
  • watch the array to update the dom. If you simply use ng-repeat to show the data, that watch will be set up automatically. If you do "manual" DOM manipulation you must use $scope.$watch() yourself.
share|improve this answer
    
Thanks for your response Stephen, – V_R Oct 8 '14 at 12:40
    
The last point sounds good and I am using this resource data something like. Calling some function from DOM and this functions are making use of this resources. <span>{{getResourceOneData()}}</span>. – V_R Oct 8 '14 at 12:42
    
Not quite. Have your worked through the tutorial at docs.angularjs.org/tutorial ? Use a model in html like <div ng-repeat=item in dataLoadedFromResource>{{item.someField}}...". Fill the "dataLoadedFromResource" in the response. – Stephen Friedrich Oct 9 '14 at 6:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.