How to Parse JSON response into ng-repeat correctly in Angularjs -
i want able use ng-repeat
in order parse response on front end. having trouble parsing responses have multiple items versus single item using ng-repeat
list.
i able parse; have create 2 different list separate ng-repeat
configuration on front end , add ugly logic not display if length of array greater one.
my goal have 1 ng-repeat
element in partial , handles both responses or better approach handle requirement.
detailed explanation , jsfiddle below.
want use ng-repeat setup both json responses.
<ul ng:repeat="report in reportconfigured.reports"> <li ng:repeat="reportitem in report">{{reportitem.reportname.$}}</li> </ul>
below response webservice when there multiple reports.
{ "reports": { "@xmlns": { "$": "http:\/\/ws.wso2.org\/dataservice" }, "report": [{ "reportid": { "$": "20" }, "reportname": { "@xmlns": { "$": "null" }, "$": "examination results" }, "visibletopartner": { "$": "false" }, "reporttype": { "@xmlns": { "$": "null" }, "$": "examination report" }, "templateid": { "$": "9" } }, { "reportid": { "$": "163" }, "reportname": { "@xmlns": { "$": "null" }, "$": "scheduled candidates test center" }, "visibletopartner": { "$": "false" }, "reporttype": { "@xmlns": { "$": "null" }, "$": "examination report" }, "templateid": { "$": "220" } }, { "reportid": { "$": "212" }, "reportname": { "@xmlns": { "$": "null" }, "$": "survey report test" }, "visibletopartner": { "$": "false" }, "reporttype": { "@xmlns": { "$": "null" }, "$": "examination report" }, "templateid": { "$": "269" } }] } };
i response service when there 1 report
{ "reports": { "@xmlns": { "$": "http:\/\/ws.wso2.org\/dataservice" }, "report": { "reportid": { "$": "212" }, "reportname": { "@xmlns": { "$": "null" }, "$": "survey report test" }, "visibletopartner": { "$": "true" }, "reporttype": { "@xmlns": { "$": "null" }, "$": "examination report" }, "templateid": { "$": "269" } } } }
i want able parse both responses same ng-repeat
. have attached jsfiddle more details.
i transform server before setting ng-repeat
datasource:
$http({...}) .success(function(data){ if ( angular.isarray(data.reports.report) ) { $scope.reports = data.reports.report; } else { $scope.reports = [data.reports.report]; } });
and then
<ul ng:repeat="report in reports"> <li ng:repeat="reportitem in report">{{reportitem.reportname.$}}</li> </ul>
Comments
Post a Comment