繁体   English   中英

无法从JSON填充HTML表

[英]Can't fill html table from json

请帮我从json响应中填写表格:

[
  {
    "id": 1,
    "firstName": "James",
    "nickNames": [
      {}
    ]
  },
  {
    "id": 2,
    "firstName": "Linda",
    "nickNames": [
      {
        "id": 2,
        "name": "lin"
      },
      {
        "id": 3,
        "name": "l1n"
      },
      {
        "id": 1,
        "name": "Lin"
      }
    ]
  }
]

我正在尝试像这样填写表格:

<table id="users" class="table table-striped table-hover">
  <thead>
  <tr>
    <th>#</th>
    <th>Name</th>
    <th>Nicknames</th>
  </tr>
  </thead>
  <tbody>
  <tr>
    <td>1</td>
    <td>James</td>
    <td>none</td>
  </tr>
  <tr>
    <td>2</td>
    <td>Linda</td>
    <td>lin, l1n, Lin</td>
  </tr>
  </tbody>
</table>

我已经完成的工作:

'use strict';

angular.module('myAppControllers', [])
  .controller('UserCtrl', ['$scope', 'User', function ($scope, User) {
    User.query().$promise.then(function (data) {
        $scope.users = data;
      }, function (reason) {
        console.log('Failed: ' + reason)
      }
    )
  }]);

这是我的模板:

<div class="row">
  <div class="page-header">
    <h1>Users</h1>
  </div>
  <table class="table table-striped table-hover">
    <thead>
    <tr>
      <th>#</th>
      <th>Name</th>
      <th>Nicknames</th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="user in users | orderBy:'id' track by user.id">
      <td>{{ user.id }}</td>
      <td>{{ user.firstName }}</td>
      <td ng-repeat="nickname in user.nickNames | emptyUser">{{ nickname.name }}</td>
    </tr>
    </tbody>
  </table>
</div>

嵌套数组的问题我无法将条目拆分为一个<td>标签。

ps我不能更改服务器的api。

我在StackOverflow上看到了类似的问题,但是它们与我的问题不同。

不要重复在td标签-使用span的内td

<td><span ng-repeat="nickname in user.nickNames | emptyUser">{{ nickname.name }} </span></td>

请在这里查看: http : //jsbin.com/vined/1/edit

  <table class="table table-striped table-hover">
    <thead>
    <tr>
      <th>#</th>
      <th>Name</th>
      <th>Nicknames</th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="user in data | orderBy:'id' track by user.id">
      <td>{{ user.id }}</td>
      <td>{{ user.firstName }}</td>
      <td><p ng-repeat="nickname in user.nickNames | emptyUser ">{{ nickname.name }}</p></td>
    </tr>
    </tbody>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM