简体   繁体   English

AngularJS ng-repeat over json query

[英]AngularJS ng-repeat over json query

Im using AngularJS in a project. 我在一个项目中使用AngularJS。 In my view I repeat over some employees and show them on the screen. 在我看来,我重复一些员工并在屏幕上显示它们。 That works, but I have to use the code 这是有效的,但我必须使用代码

<li ng-repeat="employee in employees.employee">

instead of 代替

<li ng-repeat="employee in employees">

Why cant I directly access the employees to loop over the different json objects? 为什么我不能直接访问员工来遍历不同的json对象?

My code snippets of the project: 我的项目代码片段:

Controller 调节器

Test.controller('EmployeeListController', function($scope, Employee) {
    $scope.employees = Employee.query();
});

HTML View HTML视图

<ul>
    <li ng-repeat="employee in employees.employee">
        <a href="mailto:{{employee.email}}" title="{{employee.email}}">{{employee.firstName}} {{employee.lastName}} ({{employee.email}})</a>
    </li>
</ul>

Factory

Test.factory('Employee', function ($resource) {
    return $resource('/TestServer/rest/employees/:employeeId', {}, {
        update: {method:'PUT'},
        query: {method:'GET', isArray:false}
    });
});

JSON Response JSON响应

{"employee":[{"created":null,"description":"TestDescription","email":"TestMail","firstName":"TestFirstName","id":"1","image":"TestImage.jpg","lastModification":null,"lastName":"TestLastName","phone":"121212121212"},{"created":null,"description":"TestDescription","email":"TestEmail","firstName":"TestFirstName","id":"2","image":"TestImage2.jpg","lastModification":null,"lastName":"TestLastName","phone":"2124343434"}]}

The repeater is repeating over arrays. 转发器重复阵列。 and in your response you are giving it an object. 在你的回答中,你给它一个对象。 The array is actually on the employee attribute (in your json response and also because of the isArray: false) 该数组实际上是在employee属性上(在你的json响应中,也是因为isArray:false)

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

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