Hello,
I always get the error "customers is not defined.
An array of customers and their orders are stored in the customersFactory. I've got two methods to get all customers and one by id. The problem seems to be in the getCustomer method of the customersFactory.
Anyone has a hint how to fix that error?
services.js
var app = angular.module('customerApp.services', []);
app.factory('customersFactory', function(){
return {
// Gibt alle Customers zurück
getCustomers: function() {
var customers = [
{
id: 1,
joined: '2022-12-02',
name: 'Kanon',
city: 'Mitaka',
orderTotal: 9.9956,
orders: [
{
id: 1,
product: 'Shoes',
total: 9.9956
}
]
},
{
id: 2,
joined: '2012-12-20',
name: 'Takashi',
city: 'Tokyo',
orderTotal: 19.9956,
orders: [
{
id: 2,
product: 'Pants',
total: 19.9956
},
{
id: 3,
product: 'Socks',
total: 0.9956
}
]
},
{
id: 3,
joined: '2000-05-06',
name: 'Kirby',
city: 'Osaka',
orderTotal: 90.9956,
orders: [
{
id: 4,
product: 'Shirts',
total: 90.9956
}
]
}
];
return customers;
},
getCustomer: function(customerId) {
for (var i = 0, len = customers.length; i < len; i++) {
if (customers[i].id === parseInt(customerId)) {
return customers[i];
}
}
return {};
}
}
});
controllers.js
var app = angular.module('customerApp.controllers', []);
app.controller('CustomersCtrl', ['$scope', 'customersFactory', function($scope, customersFactory) {
$scope.customers = [];
function init() {
// Gibt alle Customers zurück
$scope.customers = customersFactory.getCustomers();
}
init();
}]);
app.controller('OrdersCtrl', ['$scope', '$state', 'customersFactory', function($scope, $state, customersFactory) {
var customerId = $state.params.customerId;
$scope.customer = null;
function init() {
// Gibt einen Customer basierend auf der ID zurück
$scope.customer = customersFactory.getCustomer(customerId);
}
init();
}]);