angularjs - How to retrieve data from json using ng-repeat -
how can use ng-repeat retrieve empname of empid :2 category :1.and how categoryname json object.
here i'm pushing allitems data items array.similarly,how can data category :2
can please me out regarding issue ...
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" }] }] }]
my 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>
the json data provided not correct. since 'employee' list, contents must within array, getting repeated. moreover, name not uniform 'empnamename', present, guess data due spell mistake in place of 'empname'.
please find working 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" }] }] }]; });
Comments
Post a Comment