javascript - Angular - Can't get json data from $scope -


im working on google maps project can't json work

angular .module('angular-google-maps', ['uigmapgoogle-maps']) .controller('maincontroller', controller);  function controller ($scope, $http, $filter) {    $http.get('/data/markers.json').success(function(data) {      $scope.markers = data;    });     $scope.map = {       center: {         latitude: 52.339287,         longitude: 4.925805       },       zoom: 8,       markers: $scope.markers    };   $scope.options = {};  }; 

if understand correctly can data using $scope.markers right?

unfortunate not. when assign $scope.markers = data; changing object variable referencing, $scope.map.makers still referencing old value (which undefined).

two of simplest ways on come this:

1) reference variable derectly:

$http.get('/data/markers.json').success(function(data) {    $scope.map.markers = data;    $scope.markers = data;  }); 

2) watch:

$scope.markers = null; $http.get('/data/markers.json').success(function(data) {    $scope.markers = data;  });  // fired whenever makers changed $scope.$watch('makers', function(value) {      $scope.map.markers = value;  }); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -