ng-repeat没有显示任何内容

时间:2016-08-06 13:54:05

标签: angularjs angularjs-ng-repeat

这是我的代码和ng-repeat生成列表但没有显示任何内容。

<!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>

控制台中的json就像这样

[{“_ id”:“57a5877bb23cda352156315a”,“userId”:257,“name”:“Fortis”,“email”:“fortis@hospital.com”,“description”:“Fortis测试说明”, “地址”:{“postalCode”:“110088”,“state”:“DL”,“city”:“New Delhi”,“streetAddress”:“s-fortis”},“coordinate”:{“coordinates”: [77.1545846,28.7164134], “类型”: “点”}}]

1 个答案:

答案 0 :(得分:1)

看看你的代码:

首先你有

$scope.hospitals = response.data;

将$ scope.hospitals初始化为http响应中返回的内容。

但是你有

后立即
$scope.hospitals = JSON.stringify($scope.hospitals.data);

所以你用一个字符串覆盖了这个值。在字符串上使用ng-repeat没有意义。 ng-repeat用于迭代数组

你应该只进行一次初始化:

$scope.hospitals = response.data.data;