I've been working on a project involving angular factories, and I'm encountering an issue where the factory is not able to reference some of its own data. Specifically, in the code snippet below, data2 should be reading the value from testVariable but it's not functioning as expected. The data ends up being empty. In a more complex scenario, I am using this value as a Boolean to control visibility, and I keep getting the error "Unable to get property 'xxxxxx' of undefined or null reference". Can someone guide me on how to successfully reference testVariable within the factory as shown below? One important detail to note is that it must be returned inside the factory because there is a $watch set up to perform certain actions if the value changes.
Thank you.
(function() {
angular.module('myApp.Module').factory(
'MyFactory',
function() {
var factory = {
testVariable : 'testData2',
randomData : [
{
data1: 'testData1',
data2: testVariable,
data3: 'testData3'
}, {
data4 : 'testData4',
data5 : 'testData5',
data6 : 'testData6',
}
}
return factory;
});
}());