聪明的家伙PLZ帮助我...我无法将数据库列表从我的数据库显示到一个页面我必须超级错误在某处纠错我... ...
首先在我的package.json中:
{
"name": "IdeationApp",
"version": "1.0.0",
"description": "",
"main": "server.js",
"dependencies": {
"body-parser": "^1.10.2",
"express": "^4.11.1",
"mongojs": "^0.18.1"
}
尝试在list.html中获取数据:
<body ng-controller="AppCtrl">
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Number</th>
</tr>
<tr ng-repeat="x in y ">
<td>{{x.name}}</td>
<td>{{x.email}}</td>
<td>{{x.number}}</td>
<tr>
</table>
<script src="controllers/controller.js"></script>
</body>
在我的controller.js中:
var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http',
function($scope, $http) {
console.log("Hello World from controller");
var getdata = function() {
$http.get('/listentery').success(function(response) {
console.log("I got the data I requested..................");
//console.log(response);
$scope.dataa = response;
$scope.contact = "";
//console.log($scope.dataa);
//console.log($scope.contact);
});
};
$scope.list = function() {
console.log('calling list');
getdata();
$http.get('/list').success(function(response) {
console.log($scope.dataa);
});
};
终于在我的server.js中:
var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('idealist', ['idealist']);
var bodyParser = require('body-parser');
var path= require("path");
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.get('/listentery', function(req, res) {
console.log('I received a GET request');
db.idealist.find(function(err, docs) {
console.log(docs);
res.json(docs);
});
});
app.get('/list', function(req, res) {
console.log('I received a GET request');
res.sendFile((path.join(__dirname + '/public/list.html')));
});
答案 0 :(得分:1)
您的角度范围变量“y”未定义,您应该更新角度代码,如下所示:
$scope.getData = function() {
$http.get('/listentery')
.success(function(response) {
console.log("I got the data I requested..................");
$scope.data = response;
$scope.contact = "";
});
};
$scope.getData();
您应该更新您的html代码,如下所示
<body ng-controller="AppCtrl">
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Number</th>
</tr>
<tr ng-repeat="x in data">
<td>{{x.name}}</td>
<td>{{x.email}}</td>
<td>{{x.number}}</td>
<tr>
</table>
<script src="controllers/controller.js"></script>
</body>
我希望这会对你有所帮助。
更新:我对角度代码进行了一些更改。