Detailed explanation and examples of Angularjs' $http asynchronous deletion data
Some people may say that there is anything to talk about when deleting this thing. Write a deleted service and the controller will be finished just by calling it.
Well...it seems like this, but is it really that simple to implement? First of all, there are some pitfalls
How to determine whether the data is deleted successfully?
How to synchronize the content of the view database?
1. Ideas
1. Implementation method one
Delete the corresponding content in the database, and then splice the corresponding content in $scope
2. Implementation method two
Delete the corresponding content in the database, and then reload the data (that is, call the query method again. This consumption can be imagined, and it is necessary to ensure that the data is deleted first and then query)
2. Specific implementation method
Service to delete data: use asynchronously, return promise
service('deleteBlogService',//Delete the blog ['$rootScope', '$http', '$q', function ($rootScope, $http, $q) { var result = {}; = function (blogId) { var deferred = $(); $http({ headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }, url: $rootScope.$baseUrl + "/admin/blog/deleteBlogById", method: 'GET', dataType: 'json', params: { id: blogId } }) .success(function (data) { (data); ("Delete successfully!"); }) .error(function () { (); alert("Delete failed!") }); return ; }; return result; }])
Things to note in controller
Pay special attention to the execution order: make sure to reload the data after deletion is completed, otherwise the view will not be updated.
/** * Delete the blog */ $ = function (blogId) { var deletePromise = (blogId); (function (data) { if ( == 200) { var promise = ($); (function (data) { $ = ; $ = $; }); } }); };
The above is a detailed explanation of the example of Angularjs' $http asynchronous deletion of data. If you have any questions, please leave a message or go to the community of this site to communicate and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!