Angularjs's $ http asynchronous data deletion details and examples, angularjs asynchronous
Explanation of $ http asynchronous data deletion in Angularjs and Examples
Some people may say that there is something to say about deleting this thing. Write a deleted service and the controller will not be able to call it.
Well... it looks like this, but is it so easy to implement? First, there are the following pitfalls:
How can I determine whether the data is successfully deleted?
How can I synchronize the database content of a view?
1. Ideas
1. Implementation Method 1
Delete the corresponding content in the database, and then splice the corresponding content in $ scope
2. Implementation Method 2
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 ensure that the data is deleted and then queried)
2. Specific implementation methods
Service for data deletion: returns promise asynchronously.
Service ('deletelogservice', // delete a blog ['$ rootScope', '$ http',' $ Q', function ($ rootScope, $ http, $ q) {var result = {}; result. operate = function (blogId) {var deferred = $ q. defer (); $ http ({headers: {'content-type': 'application/x-www-form-urlencoded; charset = UTF-8 '}, url: $ rootScope. $ baseUrl + "/admin/blog/deleteBlogById", method: 'get', ype: 'json', params: {id: blogId }}). success (functio N (data) {deferred. resolve (data); console. log ("deleted successfully! ") ;}). Error (function () {deferred. reject (); alert (" deletion failed! ")}); Return deferred. promise ;}; return result ;}])
Precautions In controller
Pay special attention to the execution sequence: Make sure that the data is reloaded after the deletion is completed. Otherwise, the view will not be updated.
/*** Delete blog */$ scope. deleteBlog = function (blogId) {var deletePromise = deleteBlogService. operate (blogId); deletePromise. then (function (data) {if (data. status = 200) {var promise = getBlogListService. operate ($ scope. currentPage); promise. then (function (data) {$ scope. blogs = data. blogs; $ scope. pageCount = $ scope. blogs. totalPages ;});}});};
The above is an example of $ http asynchronous data deletion in Angularjs. If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article and hope to help you, thank you for your support!