Dies ist mein Code, ich versuche, den JSON-Inhalt anzuzeigen, wenn ich in der Konsole überprüft, die data
, die zurückgegeben wurde, war der gesamte HTML-Code, anstelle von JSON-Daten. Wo gehe ich falsch?Angularjs: Laden Inhalt von lokalen JSON-Datei
<html ng-app="BooksApp">
<head>
<meta charset="utf-8">
<title>Angular.js </title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script>
var BooksApp = angular.module('BooksApp', []);
BooksApp.controller("BookCtrl", ['$scope', '$http', function ($scope, $http) {
$http.get('books.json').success(function (data) {
$scope.books = data;
console.log(data);
});
}]);
</script>
</head>
<body ng-controller="BookCtrl">
<h2>Angular.js </h2>
<table>
<tr>
<th>Book Name</th>
<th>Book Price</th>
</tr>
<option ng-repeat="entry in books.books" value="{{entry.name}}">{{entry.name}}</option>
</table>
</body>
</html>
books.json
{
"books": [{
"id": 2081,
"name": "python",
"price": "1000"
}, {
"id": 8029,
"name": "c++",
"price": "2000"
}],
"count": 2
}
app.js
var http = require('http'),
fs = require('fs');
fs.readFile('./index.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8000);
});
zu diesem Server nennen, was Sie tun meine * "es ist der ganze HTML Code" *? Beispiel zeigen, was genau Sie sehen – charlietfl
@charlietfl Der erste Code auf der Oberseite, die ich eingefügt habe !! – MrRobot9
@charlietfl: Es funktioniert völlig in Ordnung, wenn ich $ scope.books direkt zuweisen JSON – MrRobot9