如何使用ng-repeat从json检索数据

时间:2016-02-08 04:28:35

标签: angularjs json

如何使用ng-repeat从类别中检索EmpId:2的EmpName:1。如何从此json对象获取categoryName。

这里我将allItems数据推送到items数组中。同样,我如何从类别中获取数据:2

任何人都可以帮我解决这个问题...

Myjson:

$scope.items = [];
        $scope.allItems = [{

        "categoryId": 1,
        "categoryName": "abc",
        "Employee": [{
            "EmpId": 1,
            "EmpName": "Raj",
            "EmpJob": "Accounts",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }]
        }],
        "Employee": [{
            "EmpId": 2,
            "EmpName": "sai",
            "EmpJob": "Accounts",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }]
        }]
    },
    {
        "categoryId": 2,
        "categoryName": "xyz",
        "Employee": [{
            "EmpId": 1,
            "EmpNameName": "Sita",
            "EmpJob": "Software Engineer",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }]
        }],
        "Employee": [{
            "EmpId": 2,
            "EmpName": "sathya",
            "EmpJob": "Accounts",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }]
        }]

}]

我的HTML:

 <div id="Div1" ng-repeat="item in items">
                                <div id="headerDiv3" style="margin-top: 35px;">
                                    <div id="titleDiv3"><font color="white">{{item.Employee[0].EmpName}}</font></div>
                                </div>
                                <div id="valuesDiv3">
                                    <div id="yearDiv3"><font color="white">{{item.Employee[0].year}} </font></div>
                                    <div id="salaryDiv3"><font color="white">{{item.Employee[0].salary}}</font></div>

                                    </div>
                                </div>


                            </div>

3 个答案:

答案 0 :(得分:0)

首先,您的JSON数组不正确,它应该像 -

 $scope.allItems = [{

        "categoryId": 1,
        "categoryName": "abc",
        "Employee": [{
            "EmpId": 1,
            "EmpName": "Raj",
            "EmpJob": "Accounts",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }],
       {
            "EmpId": 2,
            "EmpName": "sai",
            "EmpJob": "Accounts",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }]
        }],
    {
        "categoryId": 2,
        "categoryName": "xyz",
        "Employee": [{
            "EmpId": 1,
            "EmpNameName": "Sita",
            "EmpJob": "Software Engineer",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }],
       {
            "EmpId": 2,
            "EmpName": "sathya",
            "EmpJob": "Accounts",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }]
        }]

}]

现在,您必须先为allItems迭代两次,对allItems.Employee进行第二次迭代。

在html中:

<div id="Div1" ng-repeat="items in allItems" >
     <div id="Div2" ng-repeat="item in items.Employee>
          <div id="Div3"><font color="white">{{item.EmpName}}</font></div>
     </div>
</div>

答案 1 :(得分:0)

提供的json数据不正确。由于'Employee'是一个列表,所以它的内容必须在一个数组中,但它会重复。此外,名称不统一,因为'EmpNameName',存在,我想这是因为拼写错误代替'EmpName'的数据。

请找到有效的jsfiddle

<强> HTML

<div ng-app="app" ng-controller="test">
    <div id="Div1" ng-repeat="item in allItems">
        <div id="Div2" ng-repeat="emp in item.Employee">
            <div id="Div3">
                <font color="white">{{emp.EmpName}}</font>
            </div>
        </div>
    </div>
</div>

<强> JS

var app = angular.module('app', []);

app.controller('test', function($scope){
        $scope.allItems = [{
        "categoryId": 1,
        "categoryName": "abc",
        "Employee": [{
            "EmpId": 1,
            "EmpName": "Raj",
            "EmpJob": "Accounts",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }]
        },
        {
            "EmpId": 2,
            "EmpName": "sai",
            "EmpJob": "Accounts",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }]
        }]
    },
    {
        "categoryId": 2,
        "categoryName": "xyz",
        "Employee": [{
            "EmpId": 1,
            "EmpName": "Sita",
            "EmpJob": "Software Engineer",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }]
        },
        {
            "EmpId": 2,
            "EmpName": "sathya",
            "EmpJob": "Accounts",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }]
        }]    
    }];
});

答案 2 :(得分:-1)

希望这对您有正确的JSON格式,如下所示。

 <div id="Div1" ng-repeat="item in items | filter:{categoryId:1}" >
       <div id="Div2"  ng-repeat="emp in item.Employee| filter:{EmpId:2}">
            <div id="Div3"><font color="white">{{emp.EmpName}}</font></div>
       </div>
</div>


$scope.allItems = [{
        "categoryId": 1,
        "categoryName": "abc",
        "Employee": [{
            "EmpId": 1,
            "EmpName": "Raj",
            "EmpJob": "Accounts",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }]
        },
        {
            "EmpId": 2,
            "EmpName": "sai",
            "EmpJob": "Accounts",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }]
        }]
    },
    {
        "categoryId": 2,
        "categoryName": "xyz",
        "Employee": [{
            "EmpId": 1,
            "EmpName": "Sita",
            "EmpJob": "Software Engineer",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }]
        },
        {
            "EmpId": 2,
            "EmpName": "sathya",
            "EmpJob": "Accounts",
            "DataList": [{
                "year": "Oct-2015",
                "salary": "244"
            },
            {
                "year": "Jul-2015",
                "salary": "100"
            },
            {
                "year": "Jun-2015",
                "salary": "117"
            }]
        }]    
    }];