我使用http发送POST时遇到问题,因为我的data属性值来自一个数组,该数组每次可能具有不同的值。数组项以表单的形式从字段导出其值。每种形式都会改变。
这是我的控制器代码,
var app = angular.module("myApp", []);
app.controller('myCtrl',function ($scope, $http , $sce, $rootScope,$parse)
{
var items=<?php echo json_encode($columnsForArray) ?>;
//This is what items looks like
console.log(items);/*=> ["ID", "stock", "locstock", "location",
"con_date", "status", "showafterreturn", "vin", "auto_type", "import",
"race","street_rod", "special_interest", "other", "car_year", "make",
"model", "submodel", "special_edition", "body", "doors", "pc_date",
"return_date"]*/
$scope.runPHP = function () {
$http({
method: 'POST',
url: 'Connect-carinfo.php',
data: {
source: 'from_inside',
angular.forEach(items, function (value, key) {
--------
|
| });/* Here i need this as a result of the above foreach to
| be like this-
-------> selected: $scope.selected,
ID: $scope.ID,
stock: $scope.stock,
locstock: $scope.locstock,
location: $scope.location,
con_date: $scope.con_date,
status: $scope.status,
showafterreturn: $scope.showafterreturn,
vin: $scope.vin,*/
}
}).then(function (response) {
$scope.templateURL= $sce.trustAsHtml(response.data);
})
};
});
这是我的视图,其中包含附加到合并范围的输入字段。我将所有表单表单都放在名为testArr的数组中,使用下面的代码即可
public function getDisp($i, $j, $testArr)
{
for ($k = 0; $k < sizeof($testArr); $k++)
{
if ($testArr[$k][1] == $i && $testArr[$k][2] == $j)
{
$this->element = $testArr[$k][0];
$type = $testArr[$k][4];
$first = $testArr[0][0];
echo <<<HTML
<div>
<label for="$this->element" class="control-label">
$this->element:
</label><br>
HTML;
if ($testArr[$k][3] == 'yes')
{
if (substr($testArr[$k][4], 0, 4) == 'enum')
{
$this->createDrop($testArr[$k][4], $testArr[$k][3]);
$this->validation();
}
else
{
echo <<<HTML
<input type="$type" style="box-sizing: border-box;" class="form-control" name="$this->element" id="$this->element" data-ng-model="$this->element" required/>
HTML;
$this->validation();
}
}
else
{
if (substr($testArr[$k][4], 0, 4) == 'enum')
{
$this->createDrop($testArr[$k][4], $testArr[$k][3]);
}
else
echo <<<HTML
<input type="$type" style="box-sizing: border-box;" class="form-
control" name="$this->element" id="$this->element" data-ng-model="$this-
>element" />
HTML;
}
echo <<<HTML
</div>
<br>
HTML;
}
else
continue;
}
}
public function createDrop($item, $validation)
{
$edit = substr($item, 6, -2);
$results = explode("','", $edit);
if($validation === 'yes')
$drop = "<select style=\"box-sizing: border-box;\" class=\"form-
control\" name=\"$this->element\" id=\"$this->element\" data-ng-
model=\"$this->element\" required/>";
else
$drop = "<select style=\"box-sizing: border-box;\" class=\"form-
control\" name=\"$this->element\" id=\"$this->element\" data-ng-
model=\"$this->element\"/>";
$drop .= "<option value=\"\" disabled selected value>Select an
option</option>";
foreach ($results as $result) {
$drop .= "<option>$result</option>";
}
$drop .= "</select>";
$drop = stripslashes($drop);
echo <<<HTML
$drop
HTML;
}
答案 0 :(得分:1)
但是我仍然无法找到有关如何传递所有表单数据的答案。
AngularJS的规则在您的ng模型中总是有一个点。 1
<input ng-model="items.ID" />
<input ng-model="items.stock" />
<input ng-model="items.vin" />
然后在控制器中,只需发布items
对象:
$scope.items = {};
$scope.onSubmit = function(items) {
$http.post(url, items)
}
无论表单中有多少不同的项目,用于发布它的代码都保持不变。