我正在使用AngularJS和Django REST API构建CRUD应用程序。
我已经成功创建了get和post方法,但是没有得到如何放置请求的方法。我尝试了很多问题和YouTube的stackoverflow,但我无法解决它。
我当前的控制器是:
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:urlhttp://127.0.0.1:8000/api/v1/contact/1
的末尾是id
我希望你很清楚
您也可以尝试
$http({method: "PUT", url: URL}).
then(
function(response) {
}, function(response) {
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句