0

I am trying to loop through some json values outside the actual function. could anyone help me?

app.controller('MapCtrl', function($scope, $http){

$scope.contents = [];

$http.get('/data4estate/data_model.php')
    .success(function(data) {
        $scope.contents = data;
    });

for (i = 0; i < $scope.contents.length; i++) {
    console.log( $scope.contents[i].name);
}

});

georgeawg
  • 48,608
  • 13
  • 72
  • 95
ranaz
  • 97
  • 1
  • 10

2 Answers2

0

Try this code.

app.controller('MapCtrl', function ($scope, $http) {

      $scope.contents = [];

      $http.get('/data4estate/data_model.php')
          .success(function (data) {
              $scope.contents = data;
          });

      $scope.$watch('contents', function () {
          for (i = 0; i < $scope.contents.length; i++) {
              console.log($scope.contents[i].name);
          }
      });
  });
Vindhyachal Kumar
  • 1,713
  • 2
  • 23
  • 27
  • See [Why are angular $http success/error methods deprecated? Removed from v1.6?](http://stackoverflow.com/questions/35329384/) – georgeawg Mar 18 '17 at 04:54
0
$http.get('/data4estate/data_model.php').then(function(response) {
    angular.forEach(response.data, function(dataItem) {
        console.log(dataItem);

    });
    /*once you assign the result to $scope here, all the bindings
      in your view will update automatically */
    $scope.contents = response.data;
});
crizzis
  • 9,978
  • 2
  • 28
  • 47