這是我的代碼,我試圖顯示JSON內容,當我在控制檯中檢查時,返回的data
是整個html代碼,而不是json數據。我哪裏錯了?Angularjs:從本地json文件加載內容
<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);
});
你是什麼意思是*「它是整個html代碼」*?顯示你所看到的樣本 – charlietfl
@charlietfl我粘貼的第一個代碼! – MrRobot9
@charlietfl:當我直接將$ scope.books分配給JSON – MrRobot9