NodeJS AngularJS Express小demo错误
NodeJS AngularJS Express small demo error
我正在做一个在线小教程,基本上我正处于它要我使用来自控制器的 http get 显示数据的地步,而示例数据在服务器中 file.I 能够拉出controller.js 文件中的数据,但是当我尝试使用 .get 执行此操作时,它确实有效并且不在本地软管上显示任何数据。我认为问题出在我的 controller.js 文件中,但它已经调试了几个小时.. 没有真正看到问题.. 我敢肯定它有些愚蠢。
controller.js 文件
var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
console.log("Hello World from controller");
$http.get('/savinglist').success(function(response){
console.log("got data");
$scope.savinglist = response;
});
}]);
server.js 文件
var express = require('express');
var app = express();
app.use(express.static(__dirname + "/public"));
app.get('/savinglist', function (res, req){
console.log("I recieved get request")
person1= {
name: 'Tim',
price: 'tim@gmail.com',
discount:'(571) 426-1433'
};
person2 = {
name:'Liam',
price:'neason@taken2.com',
discount: '(777) 777-7777'
};
person3={
name: 'Jessie',
price:'jessie@vma.com',
discount: '(684) 426-1232'
};
var savinglist = [person1, person2, person3];
res.json(savinglist);
});
app.listen(3000);
console.log("server running on post 3000");
index.html 文件
<!DOCTYPE>
<html ng-app="myApp">
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css " integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" integrity="sha384-aUGj/X2zp5rLCbBxumKTCw2Z50WgIr1vs/PFN4praOTvYXWlVyh2UtNUU0KAUhAX" crossorigin="anonymous">
<title>How much are they saving</title>
</head>
<body>
<div class="container" ng-controller="AppCtrl">
<h1>Saving $$$$?</h1>
<table class="table">
<thread>
<tr>
<th>Name</th>
<th>Price</th>
<th>Discount</th>
</tr>
</thread>
<tbody>
<tr ng-repeat="savings in savinglist">
<td>{{savings.name}}</td>
<td>{{savings.price}}</td>
<td>{{savings.discount}}</td>
</tr>
</tbody>
</table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"> </script>
<script src="controllers/controller.js"></script>
</body>
</html>
在您的 server.js 文件中有错字。您在此处提供的回调函数需要将请求对象作为第一个参数,将响应作为第二个参数。
您写道:
app.get('/savinglist', function (res, req){
console.log("I recieved get request")
...
res.json(保存列表);
});
第一行应该改为:
app.get('/savinglist', function (req, res){
我正在做一个在线小教程,基本上我正处于它要我使用来自控制器的 http get 显示数据的地步,而示例数据在服务器中 file.I 能够拉出controller.js 文件中的数据,但是当我尝试使用 .get 执行此操作时,它确实有效并且不在本地软管上显示任何数据。我认为问题出在我的 controller.js 文件中,但它已经调试了几个小时.. 没有真正看到问题.. 我敢肯定它有些愚蠢。
controller.js 文件
var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
console.log("Hello World from controller");
$http.get('/savinglist').success(function(response){
console.log("got data");
$scope.savinglist = response;
});
}]);
server.js 文件
var express = require('express');
var app = express();
app.use(express.static(__dirname + "/public"));
app.get('/savinglist', function (res, req){
console.log("I recieved get request")
person1= {
name: 'Tim',
price: 'tim@gmail.com',
discount:'(571) 426-1433'
};
person2 = {
name:'Liam',
price:'neason@taken2.com',
discount: '(777) 777-7777'
};
person3={
name: 'Jessie',
price:'jessie@vma.com',
discount: '(684) 426-1232'
};
var savinglist = [person1, person2, person3];
res.json(savinglist);
});
app.listen(3000);
console.log("server running on post 3000");
index.html 文件
<!DOCTYPE>
<html ng-app="myApp">
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css " integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" integrity="sha384-aUGj/X2zp5rLCbBxumKTCw2Z50WgIr1vs/PFN4praOTvYXWlVyh2UtNUU0KAUhAX" crossorigin="anonymous">
<title>How much are they saving</title>
</head>
<body>
<div class="container" ng-controller="AppCtrl">
<h1>Saving $$$$?</h1>
<table class="table">
<thread>
<tr>
<th>Name</th>
<th>Price</th>
<th>Discount</th>
</tr>
</thread>
<tbody>
<tr ng-repeat="savings in savinglist">
<td>{{savings.name}}</td>
<td>{{savings.price}}</td>
<td>{{savings.discount}}</td>
</tr>
</tbody>
</table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"> </script>
<script src="controllers/controller.js"></script>
</body>
</html>
在您的 server.js 文件中有错字。您在此处提供的回调函数需要将请求对象作为第一个参数,将响应作为第二个参数。
您写道:
app.get('/savinglist', function (res, req){ console.log("I recieved get request")
...
res.json(保存列表); });
第一行应该改为: app.get('/savinglist', function (req, res){