使用ng-repeat标签和json数据

时间:2017-09-15 12:30:54

标签: javascript angularjs json

当我以JSON格式获取数据并尝试使用ng-repeat标记打印时,我收到错误

  

ng-dupes error

<table>
    <tr ng-repeat="emp in empArr">
      <td>{{emp.empcode}}</td>
      <td>{{emp.empName}}</td>
    </tr>
</table>

还检索json格式的AngularJS函数:

$scope.Show = function() {

$http.get("get_oracle_data.jsp?sqlStr=SELECT empcode,empname from emp")
    .then(function(response) {
       $scope.empArr = response.data;
});

有谁能告诉我这有什么问题?

3 个答案:

答案 0 :(得分:2)

使用trackBy来避免重复错误。

<table>
    <tr ng-repeat="emp in empArr track by $index">
    <td>{{emp.empcode}}</td>
        <td>{{emp.empName}}</td>
    </tr>
</table>

答案 1 :(得分:0)

你可以试试这个:

跟踪$ index 用于从数组列表中删除重复项:

<table>
    <tr ng-repeat="emp in empArr track by $index">
    <td>{{emp.empcode}}</td>
        <td>{{emp.empName}}</td>
    </tr>
</table>

答案 2 :(得分:-1)

你可以查看这个小提琴中的类似例子

jsfiddle

      <div ng-app='t' ng-controller='test'>
            <div ng-repeat='x in DATA'>
                <p>
                    <label>Title: </label> {{x.complaint_title}}
                </p>
            </div>
    </div>

    (function(){

        var app = angular.module('t', []);

        app.controller('test', 
          [
              '$scope', 
              function($scope)                            
              {
                $scope.DATA = [
                  {
                    "complaint_id": "1",
                    "complaint_title": "blah",
                    "complaint_desc": "gdga",               
                    "incident_date": "2015-02-28"
                  },
                  {
                    "complaint_id": "2",
                    "complaint_title": "gbsdsg",
                    "complaint_desc": "asadf",               
                    "incident_date": "2015-02-27"
                  }

                ];

                }
          ]);//controller

})();//closure