This is my template of an angular.js SPA:
这是我的angular.js SPA的模板:
<div data-id="{{id}}" ng-repeat="id in array.id">
<input type="text" name="value1" value="{{value1}}">
<input type="text" name="value2" value="{{value2}}">
</div>
The data for this template is loaded via http post request in a controller.
此模板的数据通过控制器中的http post请求加载。
Now I want to send this data back to the php-script to save it in the DB. I would like to do that in a directive, but I don't know how to send the data dynamically - as there are multiple rows and some templates differ a little bit.
现在我想将这些数据发送回php脚本以将其保存在数据库中。我想在一个指令中这样做,但我不知道如何动态发送数据 - 因为有多行和一些模板有点不同。
1 个解决方案
#1
First off... USE ng-model, it will make your life so much easier
首先...... USE ng-model,它将使您的生活变得更加轻松
<div data-id="{{id}}" ng-repeat="id in array.id">
<input type="text" name="value_{{$index}}" ng-model="id.value">
</div>
Second, within your controller, you are going to need to use $http.post to send the data to your php endpoint/script.
其次,在您的控制器中,您将需要使用$ http.post将数据发送到您的php端点/脚本。
app.controller("SomeController", function($scope, $http){
$scope.sendFunction = function (array) {
var data = {
array: array // The array of data you are passing in
}
$http.post('yourPhpEndpoint.php', data).success(function(data){
//This means that you have succeeded
}).error(function(){
//This means that it failed
})
}
})
Third, I'm going to assume that you are using a normalized database and would like to enter these arrays into a table one by one... In Your PHP File.
第三,我将假设您正在使用规范化数据库,并希望将这些数组逐个输入到表中...在您的PHP文件中。
$data = json_decode(file_get_contents('php://input'));
$submittedInfo = $data->array;
foreach($submittedInfo as $arrayItem){
// Do an insert into the database
}
#1
First off... USE ng-model, it will make your life so much easier
首先...... USE ng-model,它将使您的生活变得更加轻松
<div data-id="{{id}}" ng-repeat="id in array.id">
<input type="text" name="value_{{$index}}" ng-model="id.value">
</div>
Second, within your controller, you are going to need to use $http.post to send the data to your php endpoint/script.
其次,在您的控制器中,您将需要使用$ http.post将数据发送到您的php端点/脚本。
app.controller("SomeController", function($scope, $http){
$scope.sendFunction = function (array) {
var data = {
array: array // The array of data you are passing in
}
$http.post('yourPhpEndpoint.php', data).success(function(data){
//This means that you have succeeded
}).error(function(){
//This means that it failed
})
}
})
Third, I'm going to assume that you are using a normalized database and would like to enter these arrays into a table one by one... In Your PHP File.
第三,我将假设您正在使用规范化数据库,并希望将这些数组逐个输入到表中...在您的PHP文件中。
$data = json_decode(file_get_contents('php://input'));
$submittedInfo = $data->array;
foreach($submittedInfo as $arrayItem){
// Do an insert into the database
}