使用Angular js更新ng-repeat中的数据

时间:2016-08-11 05:53:05

标签: javascript angularjs

我在将数据更新到数据库时遇到问题。例如:在我的表中包含特定id的三行。因此,编辑时通过ng-repeat显示内容。

查看

if x:
    return a
return b

CI控制器

  <tr class="odd gradeX" ng-repeat="d in data">
       <td> <input type="text" name="tools" class="form-control" ng-model="d.po_tools" placeholder="Tools"> </td>

       <td> <input type="text" name="qnty" class="form-control" ng-model="d.po_quantity" placeholder="Quantity"> </td>
   </tr>

模型

 public function updatePurchaseDetails()
{
     $po_id = $this->uri->segment(4); 
     $data = file_get_contents('php://input');  
      $this->model->update_purchase_data($data,$data['count']);

}

如何在提交时将编辑过的数据更新为数据库。

1 个答案:

答案 0 :(得分:0)

HTML:

   <td> <input type="text" name="tools" class="form-control" ng-model="d.po_tools"  ng-keyup="submit('tools');" value="{{d.po_tools}}" placeholder="Tools"></td>

   <td> <input type="text" name="qnty" class="form-control" ng-model="d.po_quantity" ng-keyup="submit('quantity');" value="{{d.po_quantity}}" placeholder="Quantity"> </td>

JS控制器文件:

angular.module('appname').controller('myCtrl',function($scope,Service){

  if(!$scope.d) $scope.d = {};

  $scope.submit = function (type){
    var obj = {
      data : $scope.d,
      type : type
    }
    service.send(obj).then(function(success){
      console.log(success);
    },function(err){
      console.log(err);
    }).finally(function(){
      console.log('finish');
    });
  }

});

JS服务文件:

angular.module('appname').factory('Service',$http,function(){
  return {
    send : function(obj){
      var params = $.param(obj);
      var options = {
            url : 'http://example.com/upload.php',
            method: 'POST',
            data : params,
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          };
      return $http(options);
    }
  }
});