maskers maskers - 5 months ago 9
AngularJS Question

Why is my ng-repeat is only displaying first result?

I have this array of an array of objects that I am pushing onto from results I pass from a modal;

[
[
{
"id": 4,
"name": "THROTTLE TUBE",
"partno": "104-",
"oemnumber": "46019 038",
"stock": 19,
"price": "28"
},
{
"id": 28,
"name": "TACHOMETER, CUP",
"partno": "128-",
"oemnumber": "25012 011",
"stock": 7,
"price": "46"
}
]
]


I am getting this from the following;

$scope.woParts = [];

//later in my modal code - which is where the double [] is coming from
modalInstance.result.then(function(woParts) {
$scope.woParts.push(woParts);


I am then using this ng-repeat in my view;

<div ng-repeat="woPart in woParts">
<div class="col-md-4">
@{{ woPart[$index].name }}
</div>
<div class="col-md-2">
@{{ woPart[$index].oemnumber }}
</div>
<div class="col-md-2">
@{{ woPart[$index].price | currency}}
</div>
<div class="col-md-2">
<input type="number"
class="form-control"
ng-model="partQty">
</div>
</div>


This only displays the first result in the array. Why is that?
Thanks!

OzW OzW
Answer

I don't think you should push woParts into another object. Just save it to the scope: $scope.woParts = woParts;. Then there is no need to use $index in the ngRepeat, just use regular dot notation: woPart.name.

Or if you have to push woParts into another array, then you can change the ngRepeat expression to woPart in woParts[0].