簡體   English   中英

如何迭代角度json對象

[英]How to Iterate over angular json object

我創建了一個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>

正如安德魯所說,在你的HTML代碼中嘗試contact.name。

“聯系”本身就是整個對象。 你必須指定你想要使用的對象的哪個部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM