所以我正在学习AngularJS,并尝试简单地将JSON拉入表中。当我在表格行上执行“ng-repeat”时,为我的JSON中的每个字符重复...并且永远不会找到该元素。
我的INDEX
<!doctype html >
<html ng-app="usersApp" >
<head>
<title>Simple AngularJS Application</title>
<link rel="stylesheet" type="text/css" href="bootstrap.css" >
</head>
<body>
<div class="container" >
<div class="hero-unit" ng-view >
</div>
</div>
<script src="angular.min.js" ></script>
<script src="angular-resource.min.js" ></script>
<script src="main.js" ></script>
</body>
</html>
我的PARTIAL(list.html)
<table>
<thead>
<tr>
<td>ID</td>
<td>NAME</td>
<td>TYPE</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users">
<td>--></td><!-- NOTE: I added these tds to 'see' what was coming in -->
<td>{{user}}</td>
<td>{{user.id}}</td>
<td>{{user.name}}</td>
<td>{{user.type}}</td>
</tr>
</tbody>
</table>
我的JSON
[{"id":"284","name":"JAMES IS AAA-DRIVER","type":"Employee"},
{"id":"243,"name":"Brian J Adamms","type":"Employee"},
{"id":"237,"name":"Test Agent","type":"Brokerage Agent"}];
我的JS(main.js)
'use strict';
var App = angular.module( 'usersApp', ['ngResource'] );
App.config( function( $routeProvider ) {
$routeProvider.
when( '/', {
controller:userListController,
templateUrl:'list.html'
}).
when( '/user/:id', {
controller:userDetailController,
templateUrl:'detail.html'
}).
otherwise( '/' );
});
App.factory( 'myUsers', function( $resource ) {
return $resource( 'users.txt' );
});
var userListController = function( $scope, myUsers ) {
$scope.users = myUsers.query();
};
var userDetailController = function( $scope ) {
$scope.test = "testing Detail";
};
结果看起来像这样
ID NAME TYPE
--> {"0":"["}
--> {"0":"{"}
--> {"0":"\""}
--> {"0":"i"}
--> {"0":"d"}
--> {"0":"\""}
我会看看我是否可以在jsFiddle中重新创建。
感谢您的帮助
答案 0 :(得分:1)
您的JSON无效
[{"id":"284","name":"JAMES IS AAA-DRIVER","type":"Employee"},
{"id":"243,"name":"Brian J Adamms","type":"Employee"},
{"id":"237,"name":"Test Agent","type":"Brokerage Agent"}];
请注意最后一行中的"id":"237
部分(您缺少结束引号)。
我建议您使用JSONLint检查您的JSON的有效性。