Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
102
rated 0 times [  109] [ 7]  / answers: 1 / hits: 15534  / 10 Years ago, wed, april 16, 2014, 12:00:00

I have a module...



angular.module('myModule', []);


And then a factory



angular.module('myModule')
.factory('factory1', [
function() {
//some var's and functions
}
]);


And then another factory



angular.module('myModule')
.factory('factory2', [
function() {
//some var's and functions BUT I want to use some var's from factory1
}
]);


But I want to use some variables from factory1 inside factory2, how can I inject factory1 into factory2?


More From » angularjs

 Answers
-1

This is what I would do:



On Factory One



angular.module('sampleApp')
.factory('factory1', function() {
var factory1 = {};

factory1.method1 = function () {
return true;
};

factory1.method2 = function () {
return hello;
};

return factory1;
}
);


On Factory Two



angular.module('sampleApp')
.factory('factory2', ['factory1',
function(factory1) {

var factory2 = {};

factory2.method3 = function () {
return bye vs + factory1.method2();
};

return factory2;
}
]);

[#71431] Tuesday, April 15, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jacklynr

Total Points: 542
Total Questions: 120
Total Answers: 95

Location: Cape Verde
Member since Fri, Nov 27, 2020
4 Years ago
;