Reputation: 6968
I had the following in my code:
function store() {
this.products = [
new product("sku001", "Apple", "Tasty stuff", 1.00, "local"),
...
];
This worked fine for displaying all the products:
<h3>Products</h3>
<b>Display products: {{store.products}}</b>
<div class="row">
<div ng-repeat="product in store.products">
<img ng-src="@Url.Content("~/Content/images/products/{{product.sku}}.png")" alt="{{product.name}}"><p></p>
<p>{{product.description}}</p>
</div>
Products displayed:
Now instead of hardcoding the values, I want to load them from a restful api:
function store($resource) {
var Resource = $resource('/api/products/');
this.products = Resource.query();
console.log(this.products)
This also appears to work okay, but the formatting of the values is slightly different (Upper-case / differing key names):
[{"SKU":"sku001","Name":"Apple","ProductType":"local","Description":"Tasty stuff","Allowance":1073741824,"Price":1}]
I wanted to use the data I received from the API to create an array of objects which matches my current formatting but I am having some issues:
function store($resource) {
var Resource = $resource('/api/products/');
this.products = Resource.query(function (data) {
var product = {};
var productsList = []
for (i = 0; i < data.length; i++) {
product = {
sku: data[i].SKU,
name: data[i].Name,
description: data[i].Description,
price: data[i].Price,
category: data[i].ProductType
}
productsList.push(product);
}
return productsList;
});
When I run the code, this gets outputted:
As you can see, the json displayed has not been modified and as a result, the description / price are not displayed.
Does anybody know how I can modify data loaded using $resource and then display it?
Edit
controller.js
function storeController($scope, $routeParams, DataService) {
// get store and cart from service
$scope.store = DataService.store;
$scope.cart = DataService.cart;
}
app.js
var storeApp = angular.module('AngularStore', ['ngRoute', 'ngResource']).
//route stuff
}]);
storeApp.factory("DataService", function ($resource) {
var myStore = new store($resource);
var myCart = new shoppingCart("AngularStore");
// return data object with store and cart
return {
store: myStore,
cart: myCart
};
});
Upvotes: 0
Views: 113
Reputation: 329
function store($resource) {
var Resource = $resource('/api/products/');
Resource.query(function (data) {
var product = {};
var productsList = []
for (i = 0; i < data.length; i++) {
product = {
sku: data[i].SKU,
name: data[i].Name,
description: data[i].Description,
price: data[i].Price,
category: data[i].ProductType
}
productsList.push(product);
}
$scope.products = productsList;
});
}
When you use this.products = Resource.query(function (data) {...}
, this.products
receives the return value of Resource.query()
, not the return value of the callback function.
Edit
Use $scope
, not this
.
Upvotes: 1
Reputation: 136144
As of Angular 1.2, resources support promises. But they didn't change the rest of the behavior.
To leverage promises with $resource, you need to use the $promise property on the returned value.
function store($resource) {
var Resource = $resource('/api/products/');
Resource.query().$promise.then(
//success callback
function (data) {
var product = {};
var productsList = []
for (i = 0; i < data.length; i++) {
product = {
sku: data[i].SKU,
name: data[i].Name,
description: data[i].Description,
price: data[i].Price,
category: data[i].ProductType
}
productsList.push(product);
}
this.products = productsList;
},
//error call back
function(){
});
}
Thanks
Upvotes: 1