簡體   English   中英

NodeJS AngularJS Express小演示錯誤

[英]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.

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