I am having a problem displaying product information from the database using an AngularJS factory. Basically its a shopping cart application that I modified to display the products from the database instead of a hard coded array.
Heres the main application file source code (app.js) :
'use strict';
var shop = angular.module('shop', []);
// create a data service that provides a store and a shopping cart that
// will be shared by all views (instead of creating fresh ones for each view)
shop.factory("DataService",['$http', function($http) {
// create store
var myStore = new store($http);
// return data object with store and cart
return {
store: myStore
//cart: myCart ignore for now
}
}]);
// adding the config on the module
shop.config(function($routeProvider) {
$routeProvider // angular object, injected dynamically
.when('/', // we show the store on the root
{
controller: 'StoreController',
templateUrl: 'partials/store.htm'
})
.when('/cart',
{
controller: 'CartController',
templateUrl: 'partials/cart.htm'
})
.when('/checkout',
{
controller: 'CheckoutController',
templateUrl: 'partials/checkout.htm'
})
.when('/invoice',
{
controller: 'InvoiceController',
templateUrl: 'partials/invoice.htm'
})
.otherwise({ redirectTo: '/' }); // store
});
var controllers = {};
controllers.StoreController = function($scope, $routeParams, DataService) {
$scope.store = DataService.store;
console.log($scope.store);
//$scope.cart = DataService.cart;
}
The store source code (store.js) where I retrieve the data :
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) {
$http.post(this.url, { "action" : 'products', "fields" : this.fields })
.success(function(data, status) {
//$scope.result = data; // Show result from server in our <pre></pre> element
//return data;
this.products = data;
})
.error(function(data, status) {
//return data || "Request failed";
//this.status = status;
this.products = [];
});
}
store.prototype.getProduct = function (sku) {
for (var i = 0; i < this.products.length; i++) {
if (this.products[i].sku == sku)
return this.products[i];
}
return null;
}
Can anybody tell me what am I doing wrong here?
- Why cant I set my this.product variable to the database result?
- I would also like to extend the products class with more functions to save items to the database, how would I go about doing that?
Any advice much appreciated.
Regards
UPDATE
I have added the app.js code below, I am having an issue with the controller (StoreController) when accessing the data from the store class (store.js). It still shows an empty array. I have changed my code as suggested by m.e.conroy.
app.js
'use strict';
var shop = angular.module('shop', []);
shop.factory("DataService",['$http', '$q', function($http, $q) {
// create store
var myStore = new store($http, $q);
return {
store: myStore
}
}]);
// adding the config on the module
shop.config(function($routeProvider) {
$routeProvider // angular object, injected dynamically
.when('/', // we show the store on the root
{
controller: 'StoreController',
templateUrl: 'partials/store.htm'
})
.when('/cart',
{
controller: 'CartController',
templateUrl: 'partials/cart.htm'
})
.when('/checkout',
{
controller: 'CheckoutController',
templateUrl: 'partials/checkout.htm'
})
.when('/invoice',
{
controller: 'InvoiceController',
templateUrl: 'partials/invoice.htm'
})
.otherwise({ redirectTo: '/' }); // store
});
var controllers = {};
controllers.StoreController = function($scope, $http, $routeParams, DataService) {
$scope.store = DataService.store;
}
shop.controller(controllers);
this.products = this.getProducts($http)if you're going to setthis.productsin the success or error functions of the$http.postYou're not even returning anything fromgetProductssothis.productsis being set to the definition of the function.