0

I am using an angularjs array to store the record data(table rows) received by using the http.get rest API and printing using an ng-repeat. The problem is whenever there is only one record retrieved through rest API, ng-repeat doesn't print the record. ng-repeat only print the records when API retrieves more than one record.

 $scope.details=[];
  var lrequ = {
    method: 'GET',
    url: "https://example.com/execSQL?sql=mysqlquery",
    headers: {
    "Content-Type": "application/json"
    }
}
$http(lrequ).then(function(response){
    $scope.details=response.data.platform.record;
     alert($scope.details.fieldname);
     alert($scope.details[0].fieldname)
    }, function(){alert("failure");}); 

this is my ng-repeat line,

 <div class="col-lg-12" ng-repeat="dat in details | filter : { product_name : textname} as results">
    <p style="color:#4C97C8;" class="lead"><strong>{{dat.summary}}</strong></p>
</div>

I found where is the problem causing-

1.When rest api throws only one record, i shouldnt use ng-repeat instead i should use scope variable directly in the view and this alert($scope.details.fieldname) inside controller works but not alert($scope.details[0].fieldname).

2.When rest api throws more than one record, i have to use ng-repeat and this alert($scope.details[0].fieldname) works.

How to solve this? How can i show data in view in both cases?

5
  • No matter if the API returns 1 record or more, using ng-repeat is good. Please show us what is inside response.data and try removing the filter Commented Jan 13, 2017 at 11:26
  • If you know there's going to be 1 record, you can convert it to an array: Array.from(data); then your ng-repeat should work. Commented Jan 13, 2017 at 11:31
  • 1
    Seems to be a bad implementation of the rest api. If you're asking for a list of items, you should expect a list of items, even if it is only one item Commented Jan 13, 2017 at 11:32
  • After response.data assign output to $scope.details variable- alert($scope.details) gives [object object] as an response when it as 1 record. It gives [object object],[object object] as an response when it as 2 record @Weedoze Commented Jan 13, 2017 at 11:40
  • I agree with devqon, your rest api should return list of objects in both of case either it have multiple object or single object. Commented Jan 13, 2017 at 12:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.