使用Angular迭代jSON

时间:2017-06-19 13:35:37

标签: html angularjs mongodb mean

我使用Angular来迭代MongoDB中JSON文件的城市名,但我似乎无法完成它。我只想要城市名称

MongoDB中的JSON文件是:

{ 
"_id" : ObjectId("592409faf1f5831ca882ad39"), 
"city" : "Aalsmeer", 
"phoneLG" : "0101-101 101"
},
{ 
"_id" : ObjectId("592409faf1f5831ca882ad39"), 
"city" : "Amerongen", 
"phoneLG" : "0101-010 010"
}

这是我的Angular:

app.controller('DataCtrl2', function($scope,$http,$location,$routeParams){
  $scope.currentCity = function(){
   $http.get("/getdata").then(function(response){
     angular.forEach(response.data) {
       console.log(response);
     }
   });
  };
});

我在HTML中使用它:

<div ng-controller="DataCtrl2" ng-init="currentCity()">
   {{the citynames}}
</div>

1 个答案:

答案 0 :(得分:0)

试试这个:

控制器:

app.controller('DataCtrl2', function($scope,$http,$location,$routeParams) {

    loadCities();

    function loadCities() {
        $http.get("/getdata").then(function(response){
            $scope.cities = response.data;
        });
    }
});

HTML:

<div ng-controller="DataCtrl2">
    <div ng-repeat="city in cities"> {{ city.city }}</div>
</div>