我有以下controller.js
var eclassApp = angular.module('eclassApp', []);
eclassApp.controller('StudentsListCtrl', ['$scope','$http',
function($scope, $http){
$http.get('/static/js/students.json').success(function(data){
$scope.students = data;
});
$scope.orderProp = 'last_name';
}]);
我的HTML是以下
<!DOCTYPE html>
<html lang="en" ng-app="eclassApp">
<head>
<meta charset="UTF-8">
<title>Document</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js"></script>
<script src="../static/js/controller.js" type="application/javascript"></script>
</head>
<body ng-controller="StudentsListCtrl">
<h3>Class B3</h3>
<div class="row">
<div class="col-xs-3">
Search: <input type="text" ng-model="query">
</div>
<div class="col-xs-5">
<select ng-model="orderProp">
<option value="last_name">Alphabetical</option>
<option value="age">Youngest</option>
<option value="-age">Oldes</option>
</select>
</div>
</div>
<div class="row">
<ul>
<li ng-repeat="student in students | filter:query | orderBy:orderProp">
<p>{{student.name}}</p>
<p>{{student.last_name}}</p>
</li>
</ul>
</div>
</body>
</html>
我的json文件被正常访问(来自chrome的开发人员控制台)但仍然出现以下错误。
SyntaxError: Unexpected token ]
at Object.parse (native)
at pc (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:14:208)
at Zb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:76:379)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:77:237
at s (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:7:302)
at Zc (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:77:219)
at c (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:78:349)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:112:20
at l.$get.l.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:125:305)
at l.$get.l.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:122:398)
我错过了一个我看不到的tokken吗?
编辑:对不起..作为一个python开发人员,我用来在数组中的最后一个元素之后放置逗号,json数组不能正常运行:)。我忘记了json文件上的逗号。删除它并且效果很好答案 0 :(得分:0)
试试这个 -
eclassApp.controller('StudentsListCtrl', function($scope, $http){
$http.get('static/js/students.json').success(function(data){
$scope.students = data;
});
$scope.orderProp = 'last_name';
});