Angularjs Diferentes inyección de dependencia para fábricas dentro del controlador
app.service('FactoryService', function($scope, employerFactory, employeeFactory) {
var _mode = 0;
var _factories = [employerFactory, employeeFactory];
return {
setMode: function(mode) {
_mode = mode % (_factories.length - 1);// possibly 0 | 1 in this case, but inject as many factories as you wish
},
getFirstName: function() {
return _factories[_mode].firstName;
},
getLastName: function() {
return _factories[_mode].lastName;
}
}
});
Finally inject it into your controller, and call its methods
app.controller('MyController', function($scope, FactoryService) {
// select the first factory (employerFactory)
FactoryService.setMode(0);
$scope.firstName = FactoryService.getFirstName();
$scope.lastName = FactoryService.getLastName();
console.log( $scope.firstName, $scope.lastName);
// select the second factory (employeeFactory)
FactoryService.setMode(1);
$scope.firstName = FactoryService.getFirstName();
$scope.lastName = FactoryService.getLastName();
console.log( $scope.firstName, $scope.lastName);
});
SAMER SAEID