Javascript 如何在控制器中动态创建http post语句
我在使用http发送POST时遇到问题,因为我的数据属性中的值来自一个数组,该数组每次可能具有不同的值。数组项从表单中的feld派生其值。每种形式都会改变 这是我的控制器代码Javascript 如何在控制器中动态创建http post语句,javascript,php,angularjs,Javascript,Php,Angularjs,我在使用http发送POST时遇到问题,因为我的数据属性中的值来自一个数组,该数组每次可能具有不同的值。数组项从表单中的feld派生其值。每种形式都会改变 这是我的控制器代码 var app = angular.module("myApp", []); app.controller('myCtrl',function ($scope, $http , $sce, $rootScope,$parse) { var items=<?php echo json_encode($colu
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);
})
};
});
var-app=angular.module(“myApp”,[]);
app.controller('myCtrl',函数($scope,$http,$sce,$rootScope,$parse)
{
var项目=;
//这就是物品的外观
console.log(项目);/*=>[“ID”、“stock”、“locstock”、“location”,
“合同日期”、“状态”、“showafterreturn”、“vin”、“自动类型”、“导入”,
“比赛”、“街头酒吧”、“特殊兴趣”、“其他”、“汽车年”、“制造”,
“模型”、“子模型”、“特别版”、“车身”、“车门”、“pc\U日期”,
“返回日期”]*/
$scope.runPHP=函数(){
$http({
方法:“POST”,
url:“Connect carinfo.php”,
数据:{
资料来源:“来自内部”,
angular.forEach(项目、功能(值、键){
--------
|
|});/*由于上述原因,我需要这个
|像这样-
------->选定:$scope.selected,
ID:$scope.ID,
股票:$scope.stock,
locstock:$scope.locstock,
位置:$scope.location,
con_日期:$scope.con_日期,
状态:$scope.status,
showafterreturn:$scope.showafterreturn,
vin:$scope.vin*/
}
}).然后(功能(响应){
$scope.templateURL=$sce.trustAsHtml(response.data);
})
};
});
这是我的视图,其中包含附加到范围的输入字段。我在一个名为testArr的数组中有我所有的表单feld,下面的代码使用该数组
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;
}
公共函数getDisp($i,$j,$testArr)
{
对于($k=0;$kelement=$testArr[$k][0];
$type=$testArr[$k][4];
$first=$testArr[0][0];
echo createDrop($testArr[$k][4],$testArr[$k][3]);
$this->validation();
}
其他的
{
回音
HTML;
$this->validation();
}
}
其他的
{
如果(substr($testArr[$k][4],0,4)='enum')
{
$this->createDrop($testArr[$k][4],$testArr[$k][3]);
}
其他的
回音
HTML;
}
回声元素\“必需/>”;
其他的
$drop=“”;
$drop.=“选择一个
选择权”;
foreach($results作为$result){
$drop.=“$result”;
}
$drop.=”;
$drop=stripslashes($drop);
回音
但是我仍然无法找到关于如何传递所有表单数据的答案
AngularJS的规则是在ng模型中始终有一个点
无论表单中有多少不同的项,发布它的代码都是一样的。您的问题不是很清楚,但我认为您可能需要查看switch语句以实现所需的功能。我希望动态添加代码注释中提到的数据属性feilds。switch case不会是feas因为有时项目的长度可能会更大。比如现在项目的长度是8,可能是70。写70个案例是不可行的。你可以将键和值直接存储和绑定到一个对象中,所以我不理解这一点question@Alon如果你能给我举个例子作为回答,我将不胜感激请回答问题并包含视图(仅在将范围属性绑定到表单输入的相关部分)
<input ng-model="items.ID" />
<input ng-model="items.stock" />
<input ng-model="items.vin" />
$scope.items = {};
$scope.onSubmit = function(items) {
$http.post(url, items)
}