0

我想在我的当前地址数组,但它没有更新来更新新行的数据, 请参见下面的图像,我在角度新,加阵列的其他对象看最后的图像更新记录到当前的JSON数组angularjs

<tbody class="gradeX"> 
       <tr ng-repeat="x in Profile.addresses"> 
       <td><input type="text" class="form-control" id="inputDefault" ng-model='x.site_name ' name='site_name'></td> 
       <td><input type="text" class="form-control" id="inputDefault" ng-model='x.street_address ' name='street_address'></td> 
       <td><input type="text" class="form-control" id="inputDefault" ng-model='x.city ' name='city'></td> 
       <td><input type="text" class="form-control" id="inputDefault" ng-model='x.state ' name='state'></td> 
       <td><input type="text" class="form-control" id="inputDefault" ng-model='x.country ' name='country'></td> 
       <td><input type="text" class="form-control" id="inputDefault" ng-model='x.zip_code ' name='zip_code'></td> 
       <td><input type="text" class="form-control" id="inputDefault" ng-model='x.phone_number ' name='phone_number'></td> 

       <tr ng-repeat="lines in array"> 
         <td><input type="text" class="form-control" id="inputDefault" ng-model='x.site_name ' name='site_name'></td> 
         <td><input type="text" class="form-control" id="inputDefault" ng-model='x.street_address ' name='street_address'></td> 
         <td><input type="text" class="form-control" id="inputDefault" ng-model='x.city ' name='city'></td> 
         <td><input type="text" class="form-control" id="inputDefault" ng-model='x.state ' name='state'></td> 
         <td><input type="text" class="form-control" id="inputDefault" ng-model='x.country ' name='country'></td> 
         <td><input type="text" class="form-control" id="inputDefault" ng-model='x.zip_code ' name='zip_code'></td> 
         <td><input type="text" class="form-control" id="inputDefault" ng-model='x.phone_number ' name='phone_number'></td> 

              </tr> 
              </tr> 

             </tbody> 
<a href="" data-toggle="tooltip" title="Add Address" ng-click="addRow()"><i class="fa fa-plus fa-2x cust_primary" aria-hidden="true"></i></a> 

代码片段在地址阵列添加新行:要更新的文本框的值到数据库

 $scope.i = 0; 
    $scope.array = []; 
     $scope.addRow = function() { 
     $scope.i++; 
     $scope.array = []; 
     for(var i = 0; i < $scope.i; i++) { 
      $scope.array.push(i); 
     } 
    } 

代码段:

 $scope.updateProfile = function() {  

      $scope.tempObject={full_name:$scope.Profile.full_name, 
       mobile_number:$scope.Profile.mobile_number, 
      company_name:$scope.Profile.company_name, 
      designation: $scope.Profile.designation,  
      addresses: $scope.Profile.addresses, 
      payment_info: $scope.Profile.payment_info 

      }; 

    addresses: $scope.Profile.addresses, 

在下面的图片我按一下按钮(+)空文本框,以产生新的行: enter image description here

出现排后,我输入一些数据: enter image description here

当我点击更新按钮黄色突出显示新行数据不保存,它显示了旧记录我刷新后,我的网页

enter image description here

我的简单的问题。如何用新行更新地址数组? enter image description here

+1

我没有看到你在哪里发送新信息到任何数据库。你有服务发送到数据库吗?或者你只是想更新屏幕上的数组? –

+0

profile.address数组我想要新的行数据也添加 –

+0

2行我被添加后端,但我从前面添加3行,但它不添加地址数组 –

回答

1

如果我理解正确,你只需要在新的数据推到现有阵列象下面这样:

$scope.updateProfile = function(){ 
$scope.addresses.push($scope.form) 
$scope.form = {}; 
$scope.i = 0; 
} 

添加$scope.form = {}到控制器,收集新行,并更改模型在新排上“形成”。 +列名如下:

<td><input type="text" class="form-control" id="inputDefault" ng-model='form.site_name ' ></td> 
              <td><input type="text" class="form-control" id="inputDefault" ng-model='form.street_address ' ></td> 
              <td><input type="text" class="form-control" id="inputDefault" ng-model='form.city '></td> 
              <td><input type="text" class="form-control" id="inputDefault" ng-model='form.state ' ></td> 
              <td><input type="text" class="form-control" id="inputDefault" ng-model='form.country ' ></td> 
              <td><input type="text" class="form-control" id="inputDefault" ng-model='form.zip_code ' td> 
              <td><input type="text" class="form-control" id="inputDefault" ng-model='form.phone_number '></td> 

请注意,您将不得不做更多的事情来实际上将其永久添加到您的数据库中。

这里是一个Plunker

+0

不能正常工作,表单Object当我检查时为null,console.log($ scope.form) –

+0

是否定义了$ scope.form = {};在你的控制器的顶部?在给它分配任何东西之前,你必须定义它。看看Plunker中的代码。 $ scope.form是在页面呈现之前定义的。 –

+0

是控制台日志显示[] –