为什么我的ng-repeat只显示第一个结果?

时间:2016-06-17 16:53:34

标签: angularjs

我有一个对象数组的数组,我从一个模态传递的结果中推进;

[
  [
{
  "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"
}
  ]
]

我从下面得到这个;

 $scope.woParts = [];

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

然后我在我看来使用这个ng-repeat;

<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>

这只显示数组中的第一个结果。这是为什么? 谢谢!

1 个答案:

答案 0 :(得分:1)

我认为你不应该将woParts推入另一个对象。只需将其保存到范围:$scope.woParts = woParts;。然后,无需在ngRepeat中使用$index,只需使用常规点表示法:woPart.name

或者如果你必须将woParts推入另一个数组,那么你可以将ngRepeat表达式更改为woPart in woParts[0]