javascript - Angularjs 中的 ngRepeat 问题

标签 javascript jquery html angularjs servlets

重复此操作以在 UI 中显示表格。表的值是通过 servlet 从 Access DB 获取的。我的 Angular Controller 能够正确调用 servlet,并且 servlet 能够返回 json。然而 ng-repeat 抛出一个错误。

错误:[ngRepeat:iexp] 集合中的预期表达式为“item”[track by id]',但得到了“item” '.

代码如下:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>QuoteDetails</title>
<link href="css/bootstrap.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>

</head>
<body>
<div class="container" ng-app="myApp">
        <div ng-controller="MyController" ng-init="getDataFromServer()">
 <table class="table table-striped table-bordered"  id="example" style="border-collapse: collapse, width: 100%">
        <thead>
            <tr>
                <th>Quote No</th>
                <th>Quote Amt</th>
                <th>Quote Contact</th>
                <th colspan=2>Action</th>
            </tr>
        </thead>
        <tbody>
           <!--  <c:forEach  var="user" items="${users}">
                <tr>
                    <td><c:out value="${user.quoteNo}" /></td>
                    <td><c:out value="${user.quoteAmt}" /></td>
                    <td><c:out value="${user.quoteContact}" /></td>
                </tr>
            </c:forEach> -->
            <tr ng-repeat=item in user track by $index>
               <td>{{item.quoteNo}}</td>
               <td>{{item.quoteAmt}}</td>
               <td>{{item.quoteContact}}</td>
             </tr>
        </tbody>
    </table>
    </div>
    </div>
    <p><a href="UserController?action=insert">Add User</a></p>
    <script src="js/angular.js"></script>
    <script>
var app = angular.module('myApp', []);

app.controller("MyController", ['$scope', '$http', function($scope, $http) {
         $scope.test="hellos";
         console.log($scope.test);
        $scope.getDataFromServer = function() {
                $http({
                        method : 'GET',
                        url : 'UserController'
                }).success(function(data, status, headers, config) {
                        $scope.user= data;
                        console.log($scope.user);
                }).error(function(data, status, headers, config) {
                        // called asynchronously if an error occurs
                        // or server returns response with an error status.
                });

        };
}]);
</script>
</body>
</html>

您能告诉我问题是什么吗?

最佳答案

ng-repeat 值必须在引号内!

<tr ng-repeat="item in user track by $index">

关于javascript - Angularjs 中的 ngRepeat 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39121136/

相关文章:

javascript - 调平系统进度条

JavaScript insideHTML 不更新元素

javascript - 使用 jQuery 检测表单文件中的更改

jquery - 如何使用 jquery 在 html5 中创建粘性侧边栏?

html - Bootstrap 3 - 用 Iframe 100% 填充一个区域

css - 使用 CSS 更改 HTML5 输入的占位符颜色

javascript - 来自 HTML5 Intl API 的一周的第一天

javascript - 基于范围内的数据而不是完整数据集计算线性回归

javascript - jQuery,收集复选框并将选定的复选框放入数组,这可能吗?

javascript - 使用两个按钮在两个 div 之间切换的简单、非 jquery 方法是什么?