AngularJS行显示为空数据

时间:2013-11-25 19:07:39

标签: rest angularjs restangular

我正在测试angularJS。

在app.js我有

function ListCtrl($scope, Restangular) {
   Restangular.all("employee").getList().then(function(employee) {
     $scope.employee = employee;
     console.log($scope.employee.emp);
   });
}

并在html中我有

<table class="table table-striped table-condensed">
<thead>
<tr>
<th>Emp No</th>
<th>Name</th>
<th><a href="#/new"><i class="icon-plus-sign"></i></a></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="employee | filter:search | orderBy:'ename'">
<td><a href="{{employee.empno}}" target="_blank">{{employee.empno}}</a>
</td>
<td>{{employee.ename}}</td>
<td>
<a href="#/edit/{{employee.ename}}"><i class="icon-pencil"></i></a>
</td>
</tr>
</tbody>
</table>

我遇到的问题是显示空行而没有显示数据。 这可能是什么原因?

修改1

从服务器

返回JSON
{"emp":[{"empno":"7369","ename":"SMITH","hiredate":
"1980-12-17T00:00:00+03:00","job":"CLERK","mgr":"7902","sal":"800"},
{"comm":"300","empno":"7499","ename":"ALLEN","hiredate":
"1981-02-20T00:00:00+03:00","job":"SALESMAN","mgr":"7698","sal":"1600"},
{"comm":"500","empno":"7521","ename":"WARD","hiredate":
"1981-02-22T00:00:00+03:00","job":"SALESMAN","mgr":"7698","sal":"1250"},
{"empno":"7566","ename":"JONES","hiredate":
"1981-04-02T00:00:00+03:00","job":"MANAGER","mgr":"7839","sal":"2975"},
{"comm":"1400","empno":"7654","ename":"MARTIN","hiredate":
"1981-09-28T00:00:00+03:00","job":"SALESMAN","mgr":"7698","sal":"1250"},
{"empno":"7698","ename":"BLAKE","hiredate":
"1981-05-01T00:00:00+03:00","job":"MANAGER","mgr":"7839","sal":"2850"},
{"empno":"7782","ename":"CLARK","hiredate":
"1981-06-09T00:00:00+03:00","job":"MANAGER","mgr":"7839","sal":"2450"},
{"empno":"7788","ename":"SCOTT","hiredate":
"1987-04-19T00:00:00+03:00","job":"ANALYST","mgr":"7566","sal":"3000"},
{"empno":"7839","ename":"KING","hiredate":
"1981-11-17T00:00:00+03:00","job":"PRESIDENT","sal":"5000"},
{"comm":"0","empno":"7844","ename":"TURNER","hiredate":
"1981-09-08T00:00:00+03:00","job":"SALESMAN","mgr":"7698","sal":"1500"}]}
来自Chrome浏览器的

控制台日志

[Object, Object, Object, Object, Object, Object, Object, Object, 
Object, Object, Object, Object, Object, Object, route: "employee",
getRestangularUrl: function, addRestangularMethod: function, one:
function, all: function…]
0: Object
empno: "7369"
ename: "SMITH"
hiredate: "1980-12-17T00:00:00+03:00"
job: "CLERK"
mgr: "7902"
sal: "800"

1 个答案:

答案 0 :(得分:3)

基于你所包含的JSON,它看起来像$scope.employee应该包含一个名为“emp”的键,这是你打印到控制台的。您可能需要更改ng-repeat以使用它。

另外,我不熟悉你的ng-repeat表达形式。我认为他们应该遵循类似于“某事情”的形式,因此在这种情况下,您可能希望employee而不仅仅是employee in employee.emp

从更一般的意义上讲,Chrome的Angular Batarang plugin对于解决这些问题无限有用。