javascript - NodeJS AngularJS Express small demo error -
im doing small tutorial online , i'm @ point wants me display data using http controller while sample data in server file.i able pull data controller.js file when try using .get work , doesn't show data on local hose. think issue in controller.js file been few hours debugging.. not seeing issue.. i'm sure stupid.
controller.js file
var myapp = angular.module('myapp', []); myapp.controller('appctrl', ['$scope', '$http', function($scope, $http) { console.log("hello world controller"); $http.get('/savinglist').success(function(response){ console.log("got data"); $scope.savinglist = response; }); }]);
server.js file
var express = require('express'); var app = express(); app.use(express.static(__dirname + "/public")); app.get('/savinglist', function (res, req){ console.log("i recieved 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 file
<!doctype> <html ng-app="myapp"> <head> <!-- latest compiled , 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 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>
in server.js file have typo. callback function provide here needs have request object first parameter, , response second.
you wrote:
app.get('/savinglist', function (res, req){ console.log("i recieved request")
...
res.json(savinglist); });
the first line should instead read: app.get('/savinglist', function (req, res){
Comments
Post a Comment