AngularJS $ http PUT请求

时间:2019-05-09 17:56:22

标签: angularjs django-rest-framework

我正在使用AngularJS和Django REST API构建CRUD应用。

我已经成功创建了get和post方法,但是没有得到如何放置请求的方法。我尝试了stackoverflow很多问题和youtube,但我无法解决它。

我当前的控制器是:

app.controller('crudCtrl', function($scope, $http) {

  $http.get("http://127.0.0.1:8000/api/v1/contact/?format=json")
  .then(function(response) {
      $scope.contacts = response.data; //this is get method that displayed all the list of contact
  });

  $scope.formModel = {}; // this is same input.js, it is POST method to to send data to database
    $scope.onSubmit = function () {
        console.log("hey, i am submitting");
        console.log($scope.formModel);

        $http.post('http://127.0.0.1:8000/api/v1/contact/', $scope.formModel).
        success(function (data) {
            console.log(":)");
        }).error(function (data) {
            console.log(":(");
        });
    };


  $scope.selectUser = function (contact) {
        console.log(contact);                    // it will select the data exactly where you click
        $scope.clickedUser = contact;
    };

    $scope.updateUser = function (argument) { // it will get the form editable exactly which contact you clicked
    };

});

我的编辑视图是,当我单击编辑按钮时,将出现以下表格:

<form>
      <input type="text" ng-model="clickedUser.userid">
      <input type="text"  ng-model="clickedUser.name">
      <input type="text" ng-model="clickedUser.email">
      <input type="text"  ng-model="clickedUser.phone">
      <button type="submit" ng-click="updateUser()" data-dismiss="modal">Submit</button>
    </form>

需要注意的一点是,该编辑表单在客户端可以很好地工作,但它不会将数据发送到后端/ API /数据库。

有人可以告诉我我该如何$http.put吗?我尝试了w3school,youtube和stackoverflow问题。

我有很多解决方案,但我解决不了。

这是我对任何内容的api端点:http://127.0.0.1:8000/api/v1/contact/,因此,如果我想更新特定字段,则必须通过以下URL:http://127.0.0.1:8000/api/v1/contact/1,URL的末尾是id

我希望你明白

2 个答案:

答案 0 :(得分:2)

您也可以尝试

runLength <- rle(x$n < 10)$values
res <- length(runLength[runLength == TRUE])
#> res
#[1] 242

答案 1 :(得分:0)

您可以使用角度推杆吗?

请参阅:https://docs.angularjs.org/api/ng/service/ $ http#put

var clientData = {
   text: "Put this somewhere"
};
$http.put( url, clientData ).then( function( serverResponse ) {
    console.log(serverResponse);
},( error )=>{
    console.log(serverError);
});