Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>function store($http) { this.products = []; this.url = 'php/db.php'; this.fields = ['product_id', 'product_name', 'product_description', 'product_price']; this.products = this.getProducts($http); } store.prototype.getProducts = function ($http) { return $http.post(this.url, { "action": 'products', "fields": this.fields }) .success(function (data, status) { return data; }) .error(function (data, status) { return []; }); } </code></pre> <p>When you return <code>$http.post</code> you're returning the <code>promise</code> it creates, so <code>this.products</code> would contain the <code>$http</code> promise. When the call returns from the server the promise is resolved by the <code>success</code> or <code>error</code> functions returning data in those functions sets the variable <code>this.products</code> to what is returned.</p> <p>In this case <code>this.products = []</code> is getting replaced right away by the promise from <code>$http.</code> If you try to access that data in your application before the resolve happens you'll get back the promise it contains which could cause problems if you try and use it in other functions as if it contained the array you need. You can use <code>$q.when</code> to "wait" on the promise to resolve and then assign the returned data, so if you try to use <code>this.products</code> elsewhere before then it will still contain an empty array and thus code like <code>this.products.length</code> will still work instead of throwing an error. So you could do the following:</p> <pre><code>function store($http,$q) { this.products = []; this.url = 'php/db.php'; this.fields = ['product_id', 'product_name', 'product_description', 'product_price']; $q.when(this.getProducts($http)).then(function(data){ this.products = data; }); } </code></pre> <p>If you do decide to go this route, just take note that you'll need to inject <code>$q</code> into your service and then pass it on through your <code>new</code> operator during class creation. This would resolve any race situation you may have in your controller.</p> <p>Of course you could also use the <code>resolve</code> provided for the <code>.when</code> method of the <code>$routeProvider</code> to resolve and controller dependencies prior to the controller taking "control"</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload