[英]NodeJS AngularJS Express small demo error
我在线上做了一个小教程,基本上我是想让我使用示例数据在服务器文件中时使用来自控制器的http get来显示数据。我能够从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(“我收到请求”)
...
res.json(savinglist); });
第一行应改为:app.get('/ savinglist',function (req,res) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.