0

I was trying to get some data from a server using Angular Factory and use it in Directive to draw some chart and the code looks like:

ChartApp.directive('hcPie', function (getGlucoseProfileReport) { 
    function link (scope, element, attrs) {
            getGlucoseProfileReport.getGlucoseProfileReport().success(function(response) {
                scope.glucoseProfile = response.jData;  
                console.log(scope.glucoseProfile);
            });            
            console.log(scope.glucoseProfile);

The first console displays the data as needed but the second one logs out undefined. So I am unable to use the data at a global scale. I would really appreciate your help or incite

userJu
  • 3
  • 2
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Stewie Feb 15 '14 at 20:34
  • Thanks Stewie, yeah the problem is mentioned in the link you gave. I am now trying to do it the right way if I get lucky soon. – userJu Feb 16 '14 at 11:40

1 Answers1

0

You seem to be having an issue with the asynchronous nature of the service. To make sure you execute your code when you have retrieved the info, try wrapping the behavior in a $watch

scope.$watch("glucoseProfile", function(n, o){ If (n != o){ console.log(n); } });

ruedamanuel
  • 1,740
  • 1
  • 20
  • 21
  • Thanks Stewie and ruedamanuel, what you pointed out is right and now i am doing what i have to do with the data inside the success function for the time being and hope it works. – userJu Feb 16 '14 at 11:39