2016-08-06 30 views
0

Hier ist mein Code und ng-repeat generiert die Liste, zeigt aber nichts.ng-repeat zeigt nichts an

<!DOCTYPE html> 
<html lang="en"> 
<head> 


</head> 
    <body ng-app="myApp"> 

    <div ng-controller="getHospitalCtrl"> 

     <h3>my list</h3> 
      <ul> 
       <li ng-repeat="hospital in hospitals track by $index"> 
        <p class="name">{{hospital.name}}</p> 
        <p class="location">{{hospital.address.city}}</p> 
       </li>    
      </ul> 
     </div> 

    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
    <script> 
     var app = angular.module('myApp',[]); 
     app.controller('getHospitalCtrl',function($scope, $http){ 

     console.log('i m in getHospitalCtrl'); 

     $http.get("url") 
     .then(function(response){ 
      $scope.hospitals = JSON.stringify(response.data.data); 
      console.log($scope.hospitals); 
      //this console is printing the right json here 
    }); 

}); 
     </script> 

    </body> 
</html> 

Die json in der Konsole ist wie dieser

[{ "_id": "57a5877bb23cda352156315a", "userId": 257, "name": "Fortis", "E-Mail": "fortis @ Krankenhaus .com "," description ":" Fortis-Beschreibung zum Testen "," Adresse ": {" postalCode ":" 110088 "," state ":" DL "," Stadt ":" New Delhi "," streetAddress ":" s-fortis "}," koordinieren ": {" Koordinaten ": [77.1545846,28.7164134]," type ":" Punkt "}}]

+0

Ihre Json-Array ist gleich wie console.log (response.data)? –

Antwort

1

Schauen Sie sich Ihr Code:

Zuerst haben

$scope.hospitals = response.data; 

die $ scope.hospitalals zu dem initialisiert, was in der HTTP-Antwort zurückgegeben wird.

Aber sofort nach

$scope.hospitals = JSON.stringify($scope.hospitals.data); 

haben Sie sind also diesen Wert mit ein String überschreiben. Die Verwendung von ng-repeat auf einer Zeichenfolge ist nicht sinnvoll. ng-repeat wird verwendet, um über ein Array zu iterieren.

sollten Sie haben nur eine Initialisierung:

$scope.hospitals = response.data.data;