Angularjs's $ http asynchronous data deletion details and examples, angularjs asynchronous

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.