1

I am trying to load a chart using angularjs directive. I want to load the chart after data comes from server response. But scope is empty. My code is

<div class="span4" ui-if="loadingIsDone">
                                            <article id="piChart2">
                                                <section id="toolBox1">
                                                    <h3 id="toolH1"></h3>
                                                    <p id="toolD1"></p>
                                                </section>
                                                <article id="toolTip1"></article>
                                                <canvas id="canvas1" width="250" height="250" draw-chart>

                                                </canvas>
                                            </article>
                                        </div>

and my controller is

controller(){
 $scope.teamStrengths =  {};
 $scope.loadingIsDone = false;

 $http({
        url: "url", 
          method: "POST",
          data: {"userUids":uids}
         }).success(function(response){

             $scope.teamStrengths =  response.resource.strengths;//data loads successfully


             $scope.loadingIsDone = true;

             //rest of code is skipped
}

and my directive is

Directives.directive('drawChart', function() {
       return function(scope, element, attrs) {

           console.debug("element :"+element);
           console.debug("attrs :"+attrs);

             graphDraw('canvas1',scope.teamStrengths.value1,scope.teamStrengths.value2,scope.teamStrengths.value3)
         //grap loads successfully with static data



       };
     });

kindly help me, i shall be thankful

1 Answer 1

1

$http call is asynchronous. Directive need to $watch changes in the scope. Add this in your directive:

 scope.$watch('teamStrengths', function(newValue, oldValue) {
     if (newValue)
        graphDraw('canvas1',scope.teamStrengths.value1,scope.teamStrengths.value2,scope.teamStrengths.value3)
 }, true);

Or watch for $scope.loadingIsDone change:

 scope.$watch('loadingIsDone', function(newValue, oldValue) {
     if (newValue == true)
        graphDraw('canvas1',scope.teamStrengths.value1,scope.teamStrengths.value2,scope.teamStrengths.value3)
 }, true);
Sign up to request clarification or add additional context in comments.

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.