0

I have an angular application that reads some data from different APIs and I wrote multiple factories to catch them each factory should use a parameter to retrieve the data which is being provided by a factory. something like this:

var eqDetail = angular.module('eqDetail', []);
eqDetail.config(['$locationProvider', function($locationProvider) {
  $locationProvider.html5Mode({
    enabled: true,
    requireBase: false
  });
}]);


eqDetail.factory('eqInfoFactory', function($location, $http) {
  return {
    eqInfo: getEqInfo()

  }

  function getEqInfo() {
    //routines for acquiring data and sanitize data
  });
return tmp // and object contaning sanitized data           
}
});
eqDetail.factory('lastInspectionDetail', ['eqInfoFactory', function($http,
  eqInfoFactory) {
  return {
    insInfo: getInsInfo()
  }

  function getInsInfo() {
    var eq = eqInfoFactory.eqInfo;
    // get second set of data base on 'eq'

    return tmp
  }
}]);


eqDetail.controller('eqInfo', function($scope, eqInfoFactory) {
  $scope.eq = {};
  $scope.eq = eqInfoFactory.eqInfo;
  console.log($scope.eq);

});
eqDetail.controller('inspectionResult', function($scope, lastInspectionDetail) {
  $scope.insResult = lastInspectionDetail.insInfo;
  console.log($scope.insResult)
})

the problem is that eqInfoFactory.eqInfo in the second factory cames out as undefined.

Am I using factories in the right way? and how I can inject them into each other?

Aleksey Solovey
  • 4,153
  • 3
  • 15
  • 34
morteza kiaee
  • 73
  • 1
  • 1
  • 5
  • eqDetail.factory('lastInspectionDetail', [**'$http',**'eqInfoFactory', function($http, eqInfoFactory) – Igor Dimchevski May 29 '18 at 08:07
  • try this answer: https://stackoverflow.com/questions/23115620/using-a-factory-inside-another-factory-angularjs – Barouch Kandov May 29 '18 at 08:08
  • Possible duplicate of [Using a factory inside another factory AngularJS](https://stackoverflow.com/questions/23115620/using-a-factory-inside-another-factory-angularjs) – Dalorzo Feb 28 '19 at 18:53

2 Answers2

1

Angular's dependency injection needs, if it is used with the array notation (which it definately should for at least the sake of being minification safe), every dependency - so you are missing Angular's $http Service:

//should be ['$http', 'eqInfoFactory', fn(x)...]
eqDetail.factory('lastInspectionDetail', ['eqInfoFactory', function($http,
  eqInfoFactory) {
  return {
    insInfo: getInsInfo()
  }

  function getInsInfo() {
    var eq = eqInfoFactory.eqInfo;
    // get second set of data base on 'eq'

    return tmp
  }
}]);
JanS
  • 2,065
  • 3
  • 27
  • 29
  • thanks, but the `eqInfoFactory.eqInfo` is now an empty object instead of the real output of the other factory which is working fine on its controller any idea why? and also is the order of definition of factories matters? – morteza kiaee May 29 '18 at 11:15
0

You need to fix this line by adding the $http to minified list :

eqDetail.factory('lastInspectionDetail', ['eqInfoFactory', function($http,eqInfoFactory) {

change to this line:

eqDetail.factory('lastInspectionDetail', ['$http','eqInfoFactory', function($http,eqInfoFactory) {