【问题标题】:Issue with ngRepeat in AngularjsAngularjs中的ngRepeat问题
【发布时间】:2016-08-24 10:44:11
【问题描述】:

重复以在 UI 中显示表格。该表的值是从 Access DB 中获取的,并且是通过 servlet。我的角度控制器能够正确调用 servlet,并且 servlet 能够返回 json。但是 ng-repeat 抛出错误。

错误:[ngRepeat:iexp] collection[track by id] 中的“item”形式的预期表达式,但得到了“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>

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

【问题讨论】:

  • 不应该是item in users而不是item in user吗?

标签: javascript jquery html angularjs servlets


【解决方案1】:

ng-repeat 值必须在引号内!

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 2014-04-13
    相关资源
    最近更新 更多