Reputation: 181
I want to make a factory the same way I make a controller, by having a list of factory objects that do multiple things. in EX1(see code below) , I want to define multiple factories and store them all in one 'factories' object, and pass that list of 'factories' object into my controllers. The same way I do it in EX2, I define a 'controllers' object that holds my controllers and pass that into my demoApp.controllers. When I try to do what I have posted below I get this: Error: [$injector:unpr] http://errors.angularjs.org/1.3.14/$injector/unpr?p0=factoriesProvider%20%3C-%20factories%20%3C-%20SimpleController
and I'm not sure why. Please Help! I will give you brownie points.
If there is a way to do this, I could really segregate out my code by having a 'factories.js' file that would house all my factories, another file, 'controllers.js' file that holds all my controllers, etc. That would simplify my code a lot.
EX1:
var factories = {};
factories.simpleFactory = function($http){
var customers = [
{name: "John Smith", city: "Phoenix"},
{name: "John Doe", city: "New York"},
{name: "Jane Doe", city: "San Francisco"}
];
var factory = {};
factory.getCustomers = function(){
return customers;
};
return factory;
}
factories.notSimpleFactory= function($http){
var someVariable = [1,2,3,4];
var factory2 = {};
factoryTwo.getSomeVariable() = function(){
return someVariable;
}
return factory2;
}
demoApp.factory(factories);
EX2
var controllers = {};
controllers.SimpleController = function($scope,factories)
{
$scope.customers= {}
$scope.factory2Var = {}
init();
function init(){
$scope.customers = factories.simpleFactories.getCustomers();
}
callNotSimpleFactory()
function callNotSimpleFactory(){
$scope.factory2Var = factories.notSimpleFactory.getSomeVariable();
}
$scope.addCustomer= function(){
$scope.customers.push({name: $scope.newCustomer.name, city: $scope.newCustomer.city});
}};
demoApp.controller(controllers);
Upvotes: 0
Views: 531
Reputation: 470
Use this at the end for controllers like bleow and same for factories
for (var prop in controllers) {
demoApp.controller(prop, controllers[prop]);
}
Upvotes: 2