将记录更新为当前的json数组angularjs

时间:2016-11-22 19:10:26

标签: javascript angularjs angularjs-directive angularjs-scope

我想更新当前地址数组中的新行数据但不更新, 见下图,我是Angular的新手,添加其他数组对象见最后一张图片

<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

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

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您只需将新数据推送到现有数组中,如下所示:

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

$scope.form = {}添加到您的控制器以收集新行,并将新行上的模型更改为&#34;表单。&#34; +列名如下:

  <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