首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何为键值的键重复行

如何为键值的键重复行
EN

Stack Overflow用户
提问于 2017-06-19 18:44:25
回答 3查看 59关注 0票数 1

我收到了AngularJs中的json数据,并希望使用ng-重复在表中显示json数据的特定值。

json数据:

代码语言:javascript
运行
复制
{  
   "15":{  
      "Unique number":"133077",
      "Designation":"Software Engineer",
   },

   "16":{  
      "Unique number":"133079",
      "Designation":"Senior Software Engineer",
   },

   "18":{  
      "Unique number":"143688",
      "Designation":"Senior Software Engineer",
   }

} 

我想用HTML在表格中显示如下:

uniq no #.#^uniq

133077 \x{e76f}软件工程师

133079 \x{e76f}高级软件工程师

143688 \x{e76f}高级软件工程师

有什么帮助吗。提前谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-06-19 19:12:57

代码语言:javascript
运行
复制
angular.module('app',[]).controller('myCtrl', function($scope){
  $scope.members = {  
   "15":{  
      "Unique number":"133077",
      "Designation":"Software Engineer",
   },

   "16":{  
      "Unique number":"133079",
      "Designation":"Senior Software Engineer",
   },

   "18":{  
      "Unique number":"143688",
      "Designation":"Senior Software Engineer",
   }

}; 
});
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="myCtrl">
 <table>
  <thead>
    <th>SI.NO</th>
    <th>Unique number</th>
    <th>Designation</th>
  </thead>
  <tbody>
    <tr ng-repeat="member in members">
      <td>{{$index + 1}}</td>
      <td>{{member['Unique number']}}</td>
      <td>{{member.Designation}}</td>
    </tr>
  </tbody>
 </table>
</div>

票数 0
EN

Stack Overflow用户

发布于 2017-06-19 19:06:35

编辑: OP更改了添加一个额外字段以显示序列号的要求。

代码语言:javascript
运行
复制
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.data = {"15":{
"Unique number":"133077", "Designation":"Software Engineer", }, 
"16":{
"Unique number":"133079", "Designation":"Senior Software Engineer", }, 
"18":{
"Unique number":"143688", "Designation":"Senior Software Engineer", }
};
});
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div ng-app="myApp" ng-controller="myCtrl">
<table class="table table-bordered table-striped">
  <thead>
    <th>Sl. No.</th>
    <th>Unique number</th>
    <th>Designation</th>
  </thead>
  <tr ng-repeat='(key, value) in data'>
    <td>
      {{$index + 1}}
    </td>
    <td>
      {{value["Unique number"]}}
    </td>
    <td>
      {{value["Designation"]}}
    </td>
  </tr>
</table>
</div>

票数 0
EN

Stack Overflow用户

发布于 2017-06-19 19:22:41

我的解决方案基于简单易懂的新组件体系结构:

代码语言:javascript
运行
复制
(function() {
  'use strict';

  // app.js
  angular
    .module('app', [])
    .component('myApp', {
      template: '<items-table items="$ctrl.items"></items-table>',
      bindings: {},
      controller: function() {
        this.items = {
          "15": {
            "Designation": "Software Engineer",
            "Unique number": "133077"
          },
          "16": {
            "Designation": "Senior Software Engineer",
            "Unique number": "133079"
          },
          "18": {
            "Designation": "Senior Software Engineer",
            "Unique number": "143688"
          }
        };
      }
    });

  const itemsTableTemplate = `
<div class="items">
  <h2>Items</h2>

  <table>
    <thead>
      <tr>
        <th>unique no</th>
        <th>designation</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="(id, item) in $ctrl.items track by id">
        <td ng-bind="item['Unique number']"></td>
        <td ng-bind="item['Designation']"></td>
      </tr>
    </tbody>
  </table>
</div>
`;

  // items-table.component.js
  angular
    .module('app')
    .component('itemsTable', {
      template: itemsTableTemplate,
      bindings: {
        items: '<'
      },
      controller: ItemsTableController,
      controllerAs: '$ctrl'
    });

  function ItemsTableController() {
    // Constructor
  }
  
  angular.bootstrap(document.getElementById('app'), ['app'], {
    strictDi: true
  })
  
})();
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<div id="app">
  <my-app>
    Loading...
  </my-app>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44637769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档