所以我正在学习 AngularJS,并尝试将 JSON 简单地拉入表格中。当我在表格行上执行“ng-repeat”时,为我的 JSON 中的每个字符重复......并且永远找不到元素。
我的索引
<!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>
我的部分(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 中重新创建。
谢谢你的帮助