javascript - AngularJS: variable $scope inside a function appears undefined outside the function -


i new angularjs , stuck :\ need help, please!

the purpose of code below specific data (an integer) db in order build doughnut chart. run function dataforgraphs() in order value. then, outside function, "loose" value.

(during code, made comments explain better situation)

uonecontrollers.controller('homectrl', ['$scope', 'graphservice', function ($scope, graphservice) {       var ammountoffinishedorders = {};      dataforgraphs(); // calling function      function dataforgraphs() {         graphservice.getammountoffinishedorders()             .success(function (data) {                 $scope.ammountoffinishedorders = data.getammountoffinishedordersresult;             //this console.log shows me value 3. excellent!             console.log("value 1:", $scope.ammountoffinishedorders)              })             .error(function (error) {                 $scope.status = 'unable load data:' + error.message;             });             //however... here undefined             console.log("value 2:", $scope.ammountoffinishedorders)     };              //here, outside function, :(             console.log("value 3:", $scope.ammountoffinishedorders)              //building doughnut graphic     $scope.labels = ["january", "february", "march", "april", "may", "june", "july"];        //the problem here.  $scope.ammountoffinishedorders undefined! why??     $scope.data = [       $scope.ammountoffinishedorders, 4, 1, 3, 2, 1, 4     ];  }]); 

i tried return $scope.ammountoffinishedorders, still nothing. problem of scopes inheritance? if yes, should in order solve this? if no... well, me anyway :d

many thanks!

you updating ammountoffinishedorders in ajax call (asynchronous) , trying access in script prior being updated i.e. prior response received. because of not able value.

so, should move code i.e. $scope.data inside success callback function.

.success(function (data) {        $scope.ammountoffinishedorders = data.getammountoffinishedordersresult;             // move code here  }) 

you can call function in success callback function , updates in function.

.success(function (data) {            $scope.ammountoffinishedorders = data.getammountoffinishedordersresult;                 updatescope();      })  var updatescope = function() {     // code here }; 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -