angularjs将值推送到嵌套的ng-repeat

时间:2014-01-29 05:04:02

标签: angularjs

我有一个ng-repeat嵌套在另一个ng-repeat中。我想在单击按钮时将一些值推送到第二个数组

<div ng-repeat="vehicle in vehicleList">
  <div>{{vehicle.number}}</div>
  <input name="addName" value="add" ng-click="addVehicle(vehicle)"
  <div ng-repeat="categoryList in vehicle.category">
    <div>{{categoryList.name}}</div>
  </div>
</div>

I tried following code but it's not working
$scope.vehicleList=[];

$scope.addVehicle = function(){
 $scope.vehicleList.push({
   category:'car'
 });
}

任何人都可以帮助我。感谢

1 个答案:

答案 0 :(得分:2)

<div ng-repeat="vehicle in vehicleList">
  <div>{{vehicle.number}}</div>
  <input name="addName" value="add" ng-click="addVehicle($index, vehicle)">
  <div ng-repeat="categoryList in vehicle.category">
    <div>{{categoryList.name}}</div>
  </div>
</div>

$scope.vehicleList = [];

$scope.addVehicle = function(index, vehicle) {
 $scope.vehicleList[index].category.push({
   name: vehicle
 });
};