2

This is the sample code i tried to implement my directive: donut-chart.directive

I modified this controller js (code) to get data from web api using $http.get

But here not passing data into directive it is undefined in the isolate scope. Please give suitable solution for this.

Thank you

1
  • How does a question like this get 3 up-votes... How did you modify the controller.js code? And as @brianliu states below, you must handle the async nature of JavaScript. You can't plot data you don't have yet. Commented Oct 21, 2016 at 21:20

1 Answer 1

1

$http.get is async, if you console.log('scope.data', scope.data); in donut-chart.directive will be undefined because the promise is not yet resolved.

I suggest you can add data dependency to $routeProvider to make sure all the data is fetched before route change

.when('/edit', {
  controller: 'MainCtrl',
  templateUrl: 'edit.html',
  resolve: function() {
      // your $http get data and return promise in this function
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Brian Liu for the answer but i cannot use this option in my scenario
No worries, as long as you found the solution fits your project.

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.