如何遍历Angular JSON对象

AJ_

我创建了一个nodejs应用程序,它将读取mongo Db中的所有数据库。我能够在控制台中执行此操作。但是,当我也尝试将数据解析为json对象并将其分配到屏幕上时,我无法设法显示该信息。希望有人能帮助我弄清楚怎么做或告诉我我做错了什么。谢谢

app.js

// listen for get request, aka transfers the info in mongo to client
app.get('/databases', function (req, res) {
    console.log("-- recived GET request --"); 
    db.open(function(err, db) {

      // Use the admin database for the operation
      var adminDb = db.admin();

      // List all the available databases
      adminDb.listDatabases(function(err, dbs) {
        assert.equal(null, err);
        assert.ok(dbs.databases.length > 0);
        console.log(dbs);
        res.json(dbs); 
        db.close();
      });
    });
}); 

controller.js

var app = angular.module('myApp', []);

app.controller('customersCtrl', function($scope, $http) {
    console.log("controller connected");

function refresh(){ 
// create route 
$http.get('/databases').success(function(response) {
    console.log("recived data requested");
    $scope.databases = response; 
  });
}

// Call refresh to get req
refresh(); 

});// Controller 

index.html

<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 

<ul>
  <li ng-repeat="contact in databases">
    {{ contact }}
  </li>
</ul>
 <button type="button" onclick="hitMe()">Click Me!</button> 
</div>

</body>

在此处输入图片说明

普诺夫

看起来您需要遍历datebases.databases对象。只要$ scope.databases是:

{
 databases: [],
 totalSize: ..,
 ..
}

您的页面上需要以下ng-repeat:

<ul>
  <li ng-repeat="contact in databases.databases">
    {{ contact.name }}
  </li>
</ul>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章