Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with ng-repeat

enter image description here var responsePromisecom = $http.get("restapi calling"); responsePromisecom.success(function(data) { console.log(data); $scope.items=data.platform.record; }); responsePromisecom.error(function(data, status, headers, config) {
alert("ajax failed"); }); };

I'm making a REST API call using $http.get(). I want to show the response data using ng-repeat in my page. When the data is an array (means if the response data contains more than one object) it works fine. But, when the response data contains only one record, ng-repeat does not work properly. I don't want to use $watch. Is there any way to do so? If the items return 1 record, ng-repeat repeats more than once without any data. If the items return 2 records ng-repeat works fine.

<div ng-repeat="item in items">
  {{item.id}}
</div>
like image 377
dockerrrr Avatar asked Feb 20 '26 00:02

dockerrrr


1 Answers

Check for length of the array and do this,

<div ng-repeat="item in items ng-if="items.length > 1">
  {{item.id}}
</div>
<div ng-repeat="item in items[0]  ng-if="items.length == 1">
 {{item.id}}
</div>

EDIT

var responsePromisecom = $http.get("restapi calling");
    responsePromisecom.success(function(data) {
        console.log(data);
        if(data.platform.record.length == 1){
           $scope.items.push(data.platform.record); 
        }
        else
        {
           $scope.items = data.platform.record; 
        }           
    });
    responsePromisecom.error(function(data, status, headers, config) {  
        alert("ajax failed");
    });
};
like image 92
Sajeetharan Avatar answered Feb 21 '26 15:02

Sajeetharan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!