从AngularJS中的$ http传递多维数组

时间:2016-06-27 19:18:14

标签: php angularjs multidimensional-array

我在AngularJS中有一个数组,我希望通过$http传递来处理PHP(后端),但我无法做到。我可以毫无问题地通过$scope.myForm.shopList,但是当我发送$scope.myForm时,php不会收到任何数据。

角:

goSoft.controller('createShop', function($scope, $http){  
  $scope.myForm = [];
  $scope.myForm.shopList = [];

  $scope.shopListAdd = [{
    cod_prod : '',
    ref_prod : '',
    cant: '',
    fabricante : '',
    fecha_vence : '',
    vr_compra:'',
    vr_venta:'',
    iva:''
  }];

  $scope.add = function(shopAdd) {
    var index = $scope.shopListAdd.indexOf(shopAdd);
    $scope.shopListAdd.splice(index, 1);
    $scope.myForm.shopList.push(angular.copy(shopAdd))


    $scope.shopListAdd.push({
      cod_prod:'',
      ref_prod:'',
      cant: '',
      fabricante : '',
      fecha_vence : '',
      vr_compra:'',
      vr_venta:'',
      iva:''
    })
  }                

  $scope.submitForm = function(){    

    $http.post('../compras/objCompra.php?type=1', {datos:$scope.myForm}).success(
      function(data){
        console.log(data);
      })
  }})

PHP:

$json = json_decode(file_get_contents("php://input"));
         print_r($json);

1 个答案:

答案 0 :(得分:0)

我通过更改$ scope.myForm = {}

中的键来解决它
$scope.myForm = {};
  $scope.myForm.shopList = [];

  $scope.shopListAdd = [{
    cod_prod : '',
    ref_prod : '',
    cant: '',
    fabricante : '',
    fecha_vence : '',
    vr_compra:'',
    vr_venta:'',
    iva:''
  }];

php和我这样做了:

print_r($data = json_decode(file_get_contents("php://input"), true));