I have a large JSON object in my controller that I want to move to a separate file. Currently, this is how I'm approaching it:
myApp.controller('smController', ['$scope', function($scope) {
...
var stadtmobilRates = {
classic: {
A: {
night: 0,
hour: 1.4,
day: 21,
week: 125,
km000: 0.2,
km101: 0.18,
km701: 0.18
},
...
}
};
I've implemented a factory and promises following the guidance provided here on Stackoverflow:
myApp.factory('stadtMobilRates', function($http) {
var promise = null;
return function() {
if (promise) {
// If we've already requested this data before,
// return the existing promise.
return promise;
} else {
promise = $http.get('stadtmobilRates.json');
return promise;
}
};
});
myApp.controller('smController', ['$scope', function($scope, stadtMobilRates) {
var stadtmobilRates = null;
stadtMobilRates().success(function(data) {
stadtmobilRates = data;
});
Now I'm encountering a
TypeError: undefined is not a function
at the stadtMobilRates().success(function(data) {
line. Why isn't the stadtMobilRates
factory being recognized even though I injected it into the controller?
Edit #1: I added the factory name to the array as advised by prawn.
myApp.controller('smController', ['$scope', 'stadtMobilRates', function($scope, stadtMobilRates) {
var stadtmobilRates = null;
stadtMobilRates().success(function(data) {
stadtmobilRates = data;
});
console.log(stadtmobilRates);
However, stadtmobilRates is still null
?
Edit #2: I created a simplified version of my app on Plunker. It works there. In my full app, which involves different routes, stadtmobilRates
remains null
. Unfortunately, creating a Plunker for the complete app with routes is not feasible. So, here is the full code on GitHub. The snippet above is from Line 159. I suspect it might be related to my routes?