Angular js json访问值名称

时间:2016-02-07 09:37:26

标签: angularjs json html-table

我正在尝试使用angularJS从json生成自动生成表,但是我如何访问其自身的值名称,例如这是json我得到

    {"data":{"selectView":[{"employeeCode":"1","employeeFirstName":"name","employeeLastName":"lastName"},
{"employeeCode":"2","employeeFirstName":"name1","employeeLastName":"lastName1"}]}}

我已经通过main.js发送到我的控制器,我已经完成了这个  $ scope.employee = employee.data.selectView;

我正在使用ng-repeat =“员工在员工”

这是对象json,我做错了什么?

我如何返回值“employeeCode”或“employeeFirstName”,我在ng-repeat上尝试了员工(key,value),但它返回整行({"employeeCode":"1","employeeFirstName":"name","employeeLastName":"lastName"}

有什么想法吗?

编辑更新

解决方案是<th ng-repeat="(key, val) in employee[0]">{{key}}</th>

4 个答案:

答案 0 :(得分:2)

解决方案是<th ng-repeat="(key, val) in employee[0]">{{key}}</th>

对于我使用的td

     <tr ng-repeat="dataItem in data" ng-click="newLocation(data[$index])">

       <td ng-repeat="(key, val) in dataItem">{{val}}</td>
   </tr>

答案 1 :(得分:1)

创建一个简单的Html表并在

上应用ng-repeat
<table>
    <tr ng-repeat="employee in employees">
      <td>{{employee.employeeCode}}</td>
      <td>{{employee.employeeFirstName}}</td>
      <td>{{employee.employeeLastName}}</td>
    </tr>
  </table>
app.js中的

将json数组分配给$ scope.employees

答案 2 :(得分:0)

在您的控制器/模块文件(.Js文件)

v8_nosnapshot

在您的HTML文件中

$scope.data = [
{
employeeCode":"1","employeeFirstName":"name","employeeLastName":"lastName"
},
{
"employeeCode":"2","employeeFirstName":"name1","employeeLastName":"lastName1"
}];

干杯:)

答案 3 :(得分:0)

由于你得到的JSON是一个数组而不是一个对象,所以你不应该使用(key,value)。 您应该直接在阵列上使用ng-repeat。 在controller中,将其分配给这样的变量:

$scope.myjson = {"data":{"selectView": [{"employeeCode":"1","employeeFirstName":"name","employeeLastName":"lastName"},
{"employeeCode":"2","employeeFirstName":"name1","employeeLastName":"lastName1"}]   }}

然后在你的HTML中,

<div ng-controller="MyCtrl">
 <table>
  <tr ng-repeat="emp in myjson.data.selectView">
    <td>{{emp.employeeCode}}</td>
    <td>{{emp.employeeDirstName}}</td>
    <td>{{emp.employeeLastName}}</td>
  </tr>
</table>