javascript - How to, in AngularJS, use $http to update persistent memory -
This is an angular introduction code from the video series, which explains how to give the controller of an angle continuously with data from memory But it only stops to be understood because of lack of understanding, adding new product reviews to a stable memory
Something seems to be about how to do this, but since I have to do angular New, I'm afraid I could not understand any of them.
I have detected the syntax to request a post using $ http, but I do not know how to fit that code in the existing code, so that 1) a new element of the review array , And 2) Update the view on completion.
I have a basic way to learn about adding new product reviews in a constant memory.
(function () {var app = angular.module ('gemStore', ['store-directives']); app.controller ('StoreController', ['$ http', function ($ Http) {var store = this; store .products = []; $ http.get ('/ store-products.json'). Success (jobs) {store.products = data;});}] ); App.controller ('review controller', function () {This.review = {}; this.addReview = function (product) {product.reviews.push (this.review); this.review = {};}} });}) ();
JSON looks like this:
[{"name": "Azurit", "description": "...", ... "Review": []}, ...]
if store -products.json
is a file only on the server, you will actually need to back up the actual backend implementation (in PHP, nodes, etc.) to update the file (or more generally just return content from the database) .
Generally you create a saving method and do not post it on every revision, however, either, depending on your backend, the implementation usually applies to $ http.put ( '/ Store-products', store.products)
Whenever you click on the "Save" button, it can usually return the same data put in, so generally There is no need to update the scene because you set it up for your state I But, if you have the possibility of concurrent editing, and returns the modified data, then it looks like you get:
$ http.put ('/ store-products' , Store.products) .success (function (data) {store.products = data;});
To add an item, it can be almost the same depending on your data model:
$ http.post ('/ store- Products', newProduct) .success (function (data) {store.products = data;});
In this case POST returns an item to add and return all the products. If there are so many products - that is, the product is more like a large database than a small set in "Documents", then any server will return the post more generally after processing:
$ Http.post ('/ store-products', newProduct) .success (function (newProductFromServer) {store.products.push (newProductFromServer); // If the newProduct was not already in the array //, Store.products [ NewProductIdx] = newProductFromServer});
If you really want to call every revision instead of saving this function, then you can use it:
$ scope. $ WatchCollection (function () {Return store.products;}, function (), call {/ * $ http.put or post here} /}}
Comments
Post a Comment