【发布时间】: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