Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Angularjs different dependency injection for factories inside controller

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); 
 });
Comment

PREVIOUS NEXT
Code Example
Javascript :: Why is the return function in my debounce function never called? Angularjs 
Javascript :: angular create spec file for existing component 
Javascript :: Changing the value of a dropdown when the value of a number box changes in AngularJS 
Javascript :: angularjs No alignment and missing item in a vertical menu 
Javascript :: AngularJS w/Prerender 404 error on home page 
Javascript :: angularjs Split date and time from api response 
Javascript :: Angular js Directive to Fire "click" event on pressing enter key on ANY element 
Javascript :: object Promise showing instead of data pulled from API call 
Javascript :: AngularJS Form validation transition to valid when some elements are not even touched yet 
Javascript :: Conditional navigation inside Tabs 
Javascript :: How to add the items from a array of JSON objects to an array in Reducer 
Javascript :: context Menus 
Javascript :: coin gecko api 
Javascript :: reduce dot notations to javascript array 
Javascript :: Express.js View "globals" 
Javascript :: jquery call service 
Javascript :: get range of items in list javascript react native 
Javascript :: Printer Errors 
Javascript :: div auto extend win righting in 
Javascript :: select final 2 indexes in JS 
Javascript :: javascript find prime numbers 
Javascript :: react router how to prevent navlink from two classes 
Javascript :: Set Up Model In MongoDB 
Javascript :: detect sound chrome extension every 1 second 
Javascript :: nested object data 
Javascript :: Update A Request() Property 
Javascript :: express dynamic api template 
Javascript :: react : calling APIs after render w error message 
Javascript :: How to redirect to login page if not logged in javascript 
Javascript :: How to fix prettier messing up your HTML on save 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =