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?

3

2 Answers 2

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
  }
}]);
Sign up to request clarification or add additional context in comments.

1 Comment

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?
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) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.