gpt4 book ai didi

javascript - 更新/创建 ng-submit 和服务

转载 作者:行者123 更新时间:2023-11-28 05:26:39 25 4
gpt4 key购买 nike

我正在尝试使用 ng-submit 在数据库中更新/创建新帖子。我使用这个 HTML。

<form ng-submit="savePost()">
<input ng-model="post.TASK">
<input ng-model="post.STATUS"">
<button type="submit"></button>
</form>

提交后,返回到有此功能的 Controller 。我使用 console.log(scope.post.task) 来显示值更新是否有效,并且它有效,但不知道如何在服务中插入多个值。

   $scope.savePost = function() {
console.log($scope.post.TASK);
//I send here ID but not sure if I can send multiples values
PostAPI.update($routeParams.id).success(function(data, status, headers, config) {
$scope.post = data;
});
}
})

我只得到一个要更新的 ID。

 this.update = function(data) {
return $http.put("ajax/updatePost.php?postID=" + data);
}

这是 php 文件。

$conexion = new PDO("mysql:host=localhost;dbname=angularcode_task;charset=utf8", "root", "");
$postID = $_GET['postID'];
//WE NEED TASK AND STATUS HERE
$sql=$conexion->query("UPDATE tasks SET task='Buuu', status=2 WHERE id='$postID'");
$json = json_encode($sql->execute());
echo $json;

最佳答案

在 put 请求中,您必须指定 data 来更新您的资源,如果您需要发送资源 id,可以通过 url 发送,也可以在同一个路径中发送您发送数据的配方。

根据Angularjs docs for $http.put ,您应该能够通过发送一个附加参数来完成这样的事情,该参数是为 PUT 请求发送的 data ,如下代码所示:

$http.put(url, data, [config]); // config is optional

例如,PostAPI 也应该接收帖子作为参数:

// controller
PostAPI.update($routeParams.id, $scope.post).then ...

// PostApiService
this.update = function (postId, data) {
return $http.put("ajax/updatePost.php?postID=" + postId, data);

此外,如果您在 post 对象中包含帖子 ID,则不需要 postId 参数,只需要 post 对象然后你在 update 方法中捕获它来构建你的 url

关于javascript - 更新/创建 ng-submit 和服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40093229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com