【问题标题】:Why is only the first row is being displayed from JSON list为什么 JSON 列表中只显示第一行
【发布时间】:2014-08-12 08:47:51
【问题描述】:

我有下表:

<div data-ng-app="myApp">
    <div data-ng-controller="MyCtrl">
        <form>
            <table>
                <tr>
                    <td><b>ID</b></td>
                    <td><b>Name</b></td>
                    <td><b>Surname</b></td>
                    <td><b>House</b></td>
                    <td><b>Address</b></td>
                    <td><b>Locality</b></td>
                    <td><b>Contact1</b></td>
                    <td><b>Contact2</b></td>
                    <td><b>Contact3</b></td>
                    <td><b>Reply</b></td>
                </tr>
                <tr><td></td></tr>
                <tr ng-repeat="telesale in telesales">
                    <td>{{telesale.ID}}</td>
                    <td>{{telesale.Name}}</td>
                    <td>{{telesale.Surname}}</td>
                    <td>{{telesale.House}}</td>
                    <td>{{telesale.Address}}</td>
                    <td>{{telesale.Locality}}</td>
                    <td>{{telesale.Contact1}}</td>
                    <td>{{telesale.Contact2}}</td>
                    <td>{{telesale.Contact3}}</td>
                    <td>{{telesale.Reply}}</td>
                </tr>
            </table>
        </form>
    </div>
  </div>

控制器:

<script type="text/javascript">
  var myApp = angular.module('myApp', []);

  myApp.controller('MyCtrl', ['$scope', '$http', function ($scope, $http) {

      GetPersons();

      function GetPersons() {
          $http({
              method: 'GET',

              url: '/api/data'
          }).
        success(function (data) {
            if (data != null || data != 'undefined') {
                console.log(data);
                $scope.telesales = data;
            }
        })
      .error(function (error) {
          $window.alert("Unable to retrieve people" + error.message);
      });
      }

  } ]);

正在从 api 控制器检索数据,该控制器返回 40 个对象的列表,但表中仅显示第一个对象。为什么会发生这种情况?

从 api 检索到的一些数据的屏幕截图(数据因隐私而被划掉)

看起来好像第二个(以及其余对象)作为位置传递,这是另一个表的外键

API 代码:

public HttpResponseMessage GetPeople()
    {
        List<CommonLayer.Telesales> list = new BusinessLayer.Telesales().getUserSession(User.Identity.Name);
        //List<CommonLayer.Localities> list = new BusinessLayer.Localities().getAllLocalities();
        if (list.Count > 1)
        {
            return new HttpResponseMessage()
            {
                Content = new StringContent(JArray.FromObject(list).ToString(), Encoding.UTF8, "application/json")
            };
        }
        else
        {
            return null;
        }
    }

Plunkr

【问题讨论】:

  • 你能提供一个 plunkr/jsfiddle 吗?
  • 我猜你的问题与.net web-api有关,而不是angularjs
  • @rikket 你在 api 中扩展你的查询吗?看起来,电话销售在您的地区来回扩展。所以你的电话销售有重复的条目。
  • 您的表行周围没有迭代器来检索多个结果。您是否在输出中将每个数据行显示为单独的表格?
  • @GermannArlington 你是什么意思?当我尝试传递一个只有 2 列且没有外键的普通列​​表时,它在没有迭代器的情况下一切正常

标签: json angularjs asp.net-web-api2


【解决方案1】:

您的问题是,当您加载Localities 导航属性时,您还加载了相关的Telesales

换句话说,整个 Telesales 表仅在您加载它的第一个对象时加载,因为它的导航属性Localities 反过来加载所有Telesales 相关到它,结果你的整个表Telesales被加载到导航属性Localities中。然后当 web-api 尝试获取其他 Telesales 对象时,它发现它们都已加载并替换为 $ref : RowId

因此,您需要重新考虑您的查询,以免加载 Localities 导航属性。

免责声明

您可以尝试在 jsoneditoronline 之类的工具中解析您的 JSON 数据,以查看您从服务器获得的内容的漂亮表示

【讨论】:

    【解决方案2】:

    请看这里http://plnkr.co/edit/fLb9VHrGNqMtouK3dMca?p=preview 这是工作,但你应该调整你的 api 以获得更好格式的数据

     function GetPersons() {
                  $http({
                      method: 'GET',
    
                      url: 'data.json'
                  }).
                success(function (data) {
                  console.log(data)
                    if (data != null || data != 'undefined') {
                        console.log(data[0].Localities.Telesales);
                        $scope.telesales = data[0].Localities.Telesales;
                    }
                })
              .error(function (error) {
                  alert("Unable to retrieve people" + error.message);
              });
              }
    

    【讨论】:

    • 逻辑上这是不正确的,或者不应该是正确的做法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多