Javascript AngularJS-另存为多行-数据来自ng repeat

Javascript AngularJS-另存为多行-数据来自ng repeat,javascript,angularjs,angularjs-ng-repeat,Javascript,Angularjs,Angularjs Ng Repeat,AngularJS的新朋友你好,我正试着把我的头绕在一些东西上 基本上,我有一个表格,可以接受一个活动的1到多个“客人”。使用ng repeat,我显示如下字段: <div ng-repeat="guest in guests"> <input type="text" ng-model="guests[$index].first_name" /> <input type="text" ng-model="guests[$index].last_name" /

AngularJS的新朋友你好,我正试着把我的头绕在一些东西上

基本上,我有一个表格,可以接受一个活动的1到多个“客人”。使用ng repeat,我显示如下字段:

<div ng-repeat="guest in guests">
  <input type="text" ng-model="guests[$index].first_name" />
  <input type="text" ng-model="guests[$index].last_name" />
  <select ng-model="guests[$index].meal" ng-options="meal.id as meal.name for meal in meals"></select>
  <select ng-model="guests[$index].rsvp">
    <option value="0">No</option>
    <option value="1">Yes</option>
  </select>
</div>
<div class="controls"><button ng-click="submit()" class="btn btn-success">Save</button></div>
现在,所有这些都非常有效——我只是在保存数据时遇到了问题。我想将每位客人(名字、姓氏、用餐和回复)保存为自己的行

如果我尝试这样做:

$scope.submit = function() {
  for(var i = 0; i < $scope.guests.length; i++){
    $scope.rsvp.first_name = $scope.guests[i].first_name;
    $scope.rsvp.last_name = $scope.guests[i].last_name;
    $scope.rsvp.meal_id = $scope.guests[i].meal;
    $scope.rsvp.rsvp = $scope.guests[i].rsvp;
    $scope.rsvp.$save();
   }
   $state.transitionTo('rsvps');
 };
$scope.submit=function(){
对于(变量i=0;i<$scope.guests.length;i++){
$scope.rsvp.first_name=$scope.guests[i]。first_name;
$scope.rsvp.last_name=$scope.guests[i]。last_name;
$scope.rsvp.fine\u id=$scope.guests[i].fine;
$scope.rsvp.rsvp=$scope.guests[i].rsvp;
$scope.rsvp.$save();
}
$state.transitiono('rsvps');
};
它创建了两行(total_in_party设置为2),但它始终是第二个人数据

感觉我很接近,我看了很多ng重复的例子,但找不到一个与我的具体案例相关的

感谢您的帮助

已解决

我完全忘记了我对资源的想法,现在每次在循环中都创建一个新的RSVP对象

$scope.submit = function() {
  for(var i = 0; i < $scope.guests.length; i++){
    $scope.rsvp = new RSVPRes.RSVP();
    $scope.rsvp.first_name = $scope.guests[i].first_name;
    $scope.rsvp.last_name = $scope.guests[i].last_name;
    $scope.rsvp.meal_id = $scope.guests[i].meal;
    $scope.rsvp.rsvp = $scope.guests[i].rsvp;
    $scope.rsvp.$save();
   }
   $state.transitionTo('rsvps');
 };
$scope.submit=function(){
对于(变量i=0;i<$scope.guests.length;i++){
$scope.rsvp=新的RSVPRes.rsvp();
$scope.rsvp.first_name=$scope.guests[i]。first_name;
$scope.rsvp.last_name=$scope.guests[i]。last_name;
$scope.rsvp.fine\u id=$scope.guests[i].fine;
$scope.rsvp.rsvp=$scope.guests[i].rsvp;
$scope.rsvp.$save();
}
$state.transitiono('rsvps');
};

移动$scope.rsvp.$save();圈外

我认为问题在于长度$范围、客人、长度。在循环中放置一个警报,并通过调试器查看.Hmmm,它将为我提供预期值(2)。它循环两次,但仅在循环完成后执行保存,而不是保存每次迭代。
id
在调用保存之间没有变化。资源将
id
字段定义为
@id
,但您的对象似乎正在使用
id
$scope.submit = function() {
  for(var i = 0; i < $scope.guests.length; i++){
    $scope.rsvp.first_name = $scope.guests[i].first_name;
    $scope.rsvp.last_name = $scope.guests[i].last_name;
    $scope.rsvp.meal_id = $scope.guests[i].meal;
    $scope.rsvp.rsvp = $scope.guests[i].rsvp;
    $scope.rsvp.$save();
   }
   $state.transitionTo('rsvps');
 };
$scope.submit = function() {
  for(var i = 0; i < $scope.guests.length; i++){
    $scope.rsvp = new RSVPRes.RSVP();
    $scope.rsvp.first_name = $scope.guests[i].first_name;
    $scope.rsvp.last_name = $scope.guests[i].last_name;
    $scope.rsvp.meal_id = $scope.guests[i].meal;
    $scope.rsvp.rsvp = $scope.guests[i].rsvp;
    $scope.rsvp.$save();
   }
   $state.transitionTo('rsvps');
 };