所以我使用 angularjs/SQL 技术从数据库中检索数据,如下所示:
$http.get("retrieveData.php").then(function(response){
$scope.tasks = response.data.tasks;
})
然后我有一个函数,允许我使用表单将新数据插入数据库:
$scope.submitTask = function(){
var description = document.getElementById("typeDescription").value;
var todayDate = document.getElementById("todayDate").value;
try{
reminder = document.getElementById("reminder").value;
}
catch(err){
reminder = "NONE";
}
var status = document.getElementsByName("selectStatus");
var statusValue;
for(i=0;i<status.length;i++){
if(status[i].checked){
statusValue = status[i].value;
}
}
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("msg").innerHTML = this.responseText;
}
};
xhttp.open("POST", "enterTask.php");
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("desc="+description+"&status="+statusValue+"&reminder="+reminder+"&todayDate="+todayDate);
}
我确定我的第一个问题是我使用的是 JavaScript 的 AJAX 而不是 Angular 的?但我不知道如何转换它。
即便如此,我也不知道如何更新 $scope.tasks
。
我在网上查看了如何使用 Angular 进行 POST,但没有找到太多信息。只是关于如何获取的教程。
请不要提供任何 JQuery。我正在使用纯 JavaScript,谢谢。
<小时/>感谢一些帮助,我对代码进行了一些重组(仍然混合 JavaScript,但我计划研究 Angular 表单)。这是我现在拥有的。
$http.get("retrieveData.php").then(function(response){
$scope.tasks = response.data.tasks;
})
$scope.submitTask = function(){
var description = document.getElementById("typeDescription").value;
var todayDate = document.getElementById("todayDate").value;
try{
reminder = document.getElementById("reminder").value;
}
catch(err){
reminder = "NONE";
}
var status = document.getElementsByName("selectStatus");
var statusValue;
for(i=0;i<status.length;i++){
if(status[i].checked){
statusValue = status[i].value;
}
}
var task = {
desc:description,
status:statusValue,
reminder:reminder,
todayDate: todayDate
}
$http.post('enterTask.php', task).then(
function(response){
$scope.tasks.push(task);
}
);
}
});
由于某种原因,我的 $scope.tasks
在添加到元素后仍然没有更新。如果我向空数据库添加一个元素,我的控制台中会出现 Angular 错误。
TypeError: Cannot read property 'push' of undefined
苏。不知道为什么会这样。
当我在推送后提醒 $scope.tasks
时,它提醒的内容比推送后实际包含的内容少 1 个(一旦数据库中有 1 个或多个元素就不会产生上述错误)。
这是我的 HTML,也许它与此有关?
<ul>
<li ng-repeat="x in tasks" ng-bind="x.Description"></li>
</ul>
<form>
<input type="text" value="{{today}}" id="todayDate">
<textarea rows="15" cols="100" name="typeDescription" id="typeDescription"></textarea>
<input type="checkbox" ng-model="setReminder" name="setReminder">Set Reminder
<input type="date" name="reminder" id="reminder" ng-if="setReminder"><br>
<input type="radio" name="selectStatus" value="CR">Client Response
<input type="radio" name="selectStatus" value="IR">Internal Response
<input type="radio" name="selectStatus" value="BD">BD Control
<input type="radio" name="selectStatus" value="OC">On Calendar<br>
<input type="submit" ng-click="submitTask();">
</form>
还有我的 php...
<?php
/*$description = json_decode($_POST['desc']);
$reminder = json_decode($_POST['reminder']);
$todayDate = json_decode($_POST['todayDate']);
$status = json_decode($POST['status']);*/
$data = json_decode(file_get_contents("php://input"));
$description = $data->desc;
$reminder = $data->reminder;
$todayDate = $data->todayDate;
$status = $data->status;
require 'databaseConnect.php';
$query="INSERT INTO TaskTracker (DATESTAMP,STATUS,DESCRIPTION,REMINDER) VALUES ('$todayDate','$status','$description','$reminder')";
mysql_query($query) or die(mysql_error());
?>
注释掉的部分不起作用,所以我使用了 file_get_contents
位。
最佳答案
在 Angular js 中发布数据非常简单,只需 2 到 3 行代码即可完成。
//first lets collect your data in an Object
var data = {
desc: description,
status: statusValue,
reminder: reminder,
todayDate: todayDate
}
//then send collected data using $http service
$http.post('enterTask.php',data).then(function(response){
$scope.tasks.push(data);//After successful HTTP request you push the data to your $scope.task array
})
关于javascript - 将新数据添加到 SQL 数据库后如何更新我的 $scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39935116/