El Hombre Sin Nombre El Hombre Sin Nombre - 1 year ago 61
AngularJS Question

Update/Create ng-submit and service

I´m trying to use ng-submit to update/create new posts in database. I use this HTML.

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

When submit, he return to controller who has this function. I use
to show if value update works, and it works but don´t know how insert multiples values in service.

$scope.savePost = function() {
//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;

I only get a ID to update.

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

And this is is php file.

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

Answer Source

In a put request you must especify the data to update your resource, if you need to send the resource id, it could be sent by url or in the same recipe you send the data.

According to Angularjs docs for $http.put, you should be able to do such thing by sending an adition parameter to be the data sent for the PUT request like the below code:

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

For Example, PostAPI should receive the post as parameter too:

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

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

Also, if you had the post id inside the post object, you wouldn't need the postId param, only the post object and then you catch it inside the update method to build your url.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download