【问题标题】:iterate through jSON with Angular使用 Angular 遍历 JSON
【发布时间】:2017-11-21 18:15:03
【问题描述】:

我正在使用 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>

【问题讨论】:

  • 不太清楚你在这里想问什么。您似乎正在使用ng-init(我可能会添加错误)来调用异步获取数据的函数,然后当数据返回时,您循环遍历它并执行console.log。在这段代码中,您实际上没有保存数据以供在 HTML 中使用。 (即将数据保存到$scope)。
  • 您能粘贴整个回复吗?

标签: html angularjs mongodb mean


【解决方案1】:

试试这个:

控制器:

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>

【讨论】:

  • 这有点像我想要的,但我怎样才能在我的控制器中使用 {{city.city}} 的结果以供进一步使用。因为我需要将城市名称与不同的值进行比较
  • 我不明白你真正想要什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
  • 2015-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
相关资源
最近更新 更多