繁体   English   中英

更新/创建ng-submit和服务

[英]Update/Create ng-submit and service

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

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

提交时,他返回具有此功能的控制器。 我使用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发送,也可以在发送data的同一配方中发送。

根据$http.put Angularjs文档 ,您应该能够通过发送adition参数来做这样的事情,该参数是为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对象中有post id,你就不需要postId param,只需要post对象,然后在update方法中捕获它来构建你的url

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM