JavaScript Saving Service Data To Global Variable in Controller data saving service

I am unable to update the geodata array inside places.success function. console.log prints JSON object inside function, however outside function it's undefined.

I want to access geodata array contents outside function call. Any code snippets will help.

app.controller('MainController', ['$scope','places', function($scope,places)     {
  $scope.mapCenter = {
  lat: 40.741934,
  lng: -74.004897,
  zoom: 17
  };
  var geodata=new Array();
  places.success(function(data) {
     geodata = data;
     console.log('inside'+geodata); //geodata is having a json value here, returning from data. 
   });
   console.log(geodata); //why geodata is still undefined here, I assigned value 
   }]);

Solved the problem:

app.controller('MainController', ['$scope','places', function($scope,places)     {
  $scope.mapCenter = {
      lat: 40.741934,
      lng: -74.004897,
      zoom: 17
    };
  var geodata=new Array();
  var callme=function(data){
    console.log('call'+data)
    $scope.mapMarkers = geodataToMarkers(data)
  }
  places.success(function(data) {
    geodata = data
    console.log('inside'+data); //geodata is having a json value here, returning from data. 
    callme(data);
  });
 }]);
Answer:1

Because places.success is asycnronous, after calling this method it move to console.log(geodata) and it will print empty array not null. You must wait for the response of having the data.

app.controller('MainController', ['$scope','places', function($scope,places)     {
  $scope.mapCenter = {
  lat: 40.741934,
  lng: -74.004897,
  zoom: 17
  };
  var geodata=new Array();
  places.success(function(data) {
     geodata = data;
      console.log('inside'+geodata); //You can receive here data
   });
    // console.log(geodata); Because places.success is asycnronous, its the empty array not null
   }]);
Answer:2

Assuming there is a directive (say my-marker) to display a geolocation on map, you may have an ng-repeat in your template html that would run through geodata variable. Hence you should define it on controller scope.

Controller code fragment

$scope.geodata = [];
places.success(function (data) {
  $scope.geodata = data; //Note depending on how your promise is resolved you may have to do **data.data** instead of **data**
});

Template

<my-marker ng-repeat="geoDatum in geoData" geo-datum="geoDatum"></my-marker>

NOTE: I have made stated assumptions around your directive.

Answer:3

I have an array named data as shown below and an array of objects named obj1. var data = ["004", "456", "333", "555"]; obj1 = [{ id: "004", name: "Rick", Active: "false" }, { id: "005", ...

I have an array named data as shown below and an array of objects named obj1. var data = ["004", "456", "333", "555"]; obj1 = [{ id: "004", name: "Rick", Active: "false" }, { id: "005", ...

How can i solve the issue of scope of variable in the below case. Myapp.controller('MyController', ['$scope', function ($scope) { var formvalues = {}; $scope.somebuttonclick = function()...

How can i solve the issue of scope of variable in the below case. Myapp.controller('MyController', ['$scope', function ($scope) { var formvalues = {}; $scope.somebuttonclick = function()...

  1. variable scope javascript issue

I created a bootstrap-compass project with the following yeoman generator: https://www.npmjs.com/package/generator-bootstrap-compass You can see the file structure in the link. How do I add the ...

I created a bootstrap-compass project with the following yeoman generator: https://www.npmjs.com/package/generator-bootstrap-compass You can see the file structure in the link. How do I add the ...

I'm using SweetAlert2 and the 7th example on this page: Go to 7th example below to see it in action this one: swal({ title: 'Are you sure?', text: 'You will not be able to ...

I'm using SweetAlert2 and the 7th example on this page: Go to 7th example below to see it in action this one: swal({ title: 'Are you sure?', text: 'You will not be able to ...

  1. sweet alert redirect after click ok
  2. sweet alert redirect on confirm
  3. sweet alert redirect after timer
  4. sweet alert redirect after confirm
  5. sweet alert redirect
  6. sweet alert redirect on ok
  7. sweet alert redirect page
  8. sweet alert redirect after ok php
  9. sweet alert redirect url
  10. php sweetalert redirect
  11. js sweetalert redirect
  12. sweet alert click ok redirect
  13. sweet alert onclick ok redirect
  14. sweet alert success redirect
  15. redirect after sweet alert
  16. sweet alert then redirect
  17. sweetalert2 redirect
  18. sweetalert2 redirect after click ok
  19. redirect sweetalert button
  20. redirect page after sweet alert