Note that there are some explanatory texts on larger screens.

plurals
  1. POAngular : Pass JSON $http promise to factory
    text
    copied!<p>At the moment, my Angular app looks like this:</p> <h3>Factory in <code>app.js</code></h3> <pre><code>StoreApp.factory("DataService", function () { // create store var myStore = new store(); // create shopping cart var myCart = new shoppingCart("Store"); // return data object with store and cart return { store: myStore, cart: myCart }; }); </code></pre> <h3><code>controller.js</code></h3> <pre><code> function storeController($scope, $http, $routeParams, $location, DataService) { $scope.store = DataService.store; $scope.cart = DataService.cart; // use routing to pick the selected product if ($routeParams.productUrlTitle != null) { $scope.product = $scope.store.getProduct($routeParams.productUrlTitle) || $scope.store.getHero($routeParams.productUrlTitle); } $scope.predicate = '-price'; $scope.store.isCart = $location.path() == "/cart"; } </code></pre> <p>In <code>store.js</code> (below) is where my issue is — currently <code>this.products[]</code> takes inline assignments. I need this to instead load an external JSON file (also below). I've tried several things from including/passing the <code>promise</code> to <code>var myStore = new store();</code>, to actually including <code>$http.get()</code> paired with <code>.then()</code> inside of <code>store.js</code> — to no avail. </p> <h3><code>store.js</code></h3> <pre><code>function store() { this.products = [ new product("USD", 20, "https://foo.jpg", "Name", "Description"), new product("USD", 20, "https://bar.jpg", "Name", "Description"), ]; } store.prototype.getProduct = function (urlTitle) { for (var i = 0; i &lt; this.products.length; i++) { if (this.products[i].urlTitle == urlTitle) return this.products[i]; } return null; } </code></pre> <h3><code>payload.json</code></h3> <pre><code>[ { "currency": "usd", "cost": 1000, "image_url": "https://whatever.domain/someimage.jpg", "id": "xyz", "name": "A title", "description": "Some details" }, ... ] </code></pre> <p>For those interested, my project is based on this: <a href="http://www.codeproject.com/Articles/576246/A-Shopping-Cart-Application-Built-with-AngularJS" rel="nofollow">A Shopping Cart Application Built with AngularJS</a>. </p> <p>Many thanks in advance. </p> <hr> <h2>Update</h2> <p>I was able to accomplish what I wanted, but I'm not certain it's the best (Read: correct) way to. In short, I added a new factory called "InventoryService" that I pass to my controller. </p> <h3><code>app.js</code></h3> <pre><code>// New Factory Added StoreApp.factory('InventoryService', ['$http', '$rootScope', function ($http, $rootScope) { var inventory = []; return { getInventory: function () { return $http.get('http://localhost/ShoppingCart/payload.json').then(function (response) { inventory = response; $rootScope.$broadcast('handleInventoryService', inventory); return inventory; }) } }; } ]); </code></pre> <h3><code>controller.js</code></h3> <pre><code>function storeController($scope, $http, $routeParams, $location, InventoryService, DataService) { $scope.name = 'inventory'; (function () { InventoryService.getInventory().then(function (inventory) { $scope.inventory = inventory; for (var i = 0; i &lt; $scope.inventory.data.length; i++) { if ($scope.inventory.data[i].id == '11ca3ea26f0e431eb996a401f292581f2') { DataService.store.hero.push( new product( $scope.inventory.data[i].id, $scope.inventory.data[i].image_url, $scope.inventory.data[i].name, $scope.inventory.data[i].description, $scope.inventory.data[i].cost ) ); } else { DataService.store.products.push( new product( $scope.inventory.data[i].id, $scope.inventory.data[i].image_url, $scope.inventory.data[i].name, $scope.inventory.data[i].description, $scope.inventory.data[i].cost ) ); } } // get store and cart from service $scope.store = DataService.store; $scope.cart = DataService.cart; ... </code></pre> <h3><code>store.html</code> partial</h3> <pre><code>&lt;div ng-include src="'partials/header.html'"&gt;&lt;/div&gt; &lt;div ng-repeat="product in store.hero" class="row-fluid"&gt; &lt;div class="span12"&gt; &lt;div class="span4"&gt; &lt;a href="#/products/{{product.urlTitle}}"&gt; &lt;img class="img-polaroid" ng-src="{{product.image_url}}" title="{{product.name}}" /&gt; &lt;/a&gt; &lt;/div&gt; &lt;div class="span8"&gt; &lt;h1 class="tango-tang weight-100"&gt; {{product.name}} &lt;/h1&gt; &lt;hr /&gt; &lt;div class="row-fluid"&gt; &lt;div class="span7"&gt; &lt;p&gt; {{product.description}} &lt;/p&gt; &lt;/div&gt; &lt;div class="span5"&gt; &lt;div class="well"&gt; &lt;h1 class="weight-300 text-center"&gt; {{product.price | currency}} &lt;/h1&gt; &lt;/div&gt; &lt;button class="btn btn-success btn-medium btn-block" ng-click="cart.addItem(product.sku, product.image_url, product.name, product.price, 1)"&gt; &lt;i class="icon-plus"&gt;&lt;/i&gt; Add to Cart &lt;/button&gt; &lt;a href="#/products/{{product.urlTitle}}" class="btn btn-block"&gt; &lt;i class="icon-list"&gt;&lt;/i&gt; Details &lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre>
 

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