0

所以我正在学习 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>--&gt;</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 中重新创建。

谢谢你的帮助

4

1 回答 1

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 的有效性。

于 2013-06-12T17:04:46.617 回答