我如何在这个JSON解析上做一个Angular JS ng-repeat?

时间:2014-11-14 13:29:15

标签: java javascript angularjs content-management-system hybrid-mobile-app

如何在此JSON解析中执行Angular JS ng-repeat?

    .controller('TeamsCtrl', function($scope) {  

    var text = '{"employees":[' +
        '{"name":"John Doe"},' +
        '{"name":"Sam Doe"},' +
        '{"name":"Jack Doe"}]}';  
    /* create scope property that will be recognized in view*/
    $scope.tasks =  JSON.parse(text);

});

以下是我想要填充新内容的区域:

    <ion-content class="has-header" ng-controller="TeamsCtrl">
  <div class="list card" ng-repeat="employee in tasks.employee" ng-click="showActionsheet()">
    <div class="item item-divider">
       <div>{{employee.name}}</div>
    </div>

4 个答案:

答案 0 :(得分:1)

您没有将解析后的结果分配给范围变量

控制器应该更像:

.controller('TeamsCtrl', function($scope) {  

    var text = '{"employees":[' +
        '{"name":"John Doe"},' +
        '{"name":"Sam Doe"},' +
        '{"name":"Jack Doe"}]}';  
    /* create scope property that will be recognized in view*/
    $scope.tasks =  JSON.parse(text);

});

然后在标记中你可以做类似的事情:

<div ng-repeat="employee in tasks.employees">
    <div>{{employee.name}}</div>
</div>

DEMO

答案 1 :(得分:0)

您已经拥有了Json对象,只需将其设置在范围内并迭代它即可

<h3>Output:</h3>
<div ng-app ng-controller="MyCtrl">
    <ul>
        <li ng-repeat="(id,emp) in items">{{id}}: {{emp.name}}</li>
    </ul>
</div>

Js Fiddle here

答案 2 :(得分:0)

如果您拥有正确的员工名单,可以使用

<div ng-repeat="employee in employees>
    <div>{{id}}:{{employee.name}}</div>
</div>

答案 3 :(得分:0)

<div ng-repeat="item in obj.employees">{{item.name}}</div>

Check my plunker