I have a huge json object in my controller that I would like to outsorce to a seperate file. So far I'm doing this:
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 have used a factory and promises as explained here on Stackoverflow:
myApp.factory('stadtMobilRates', function($http) {
var promise = null;
return function() {
if (promise) {
// If we've already asked for this data once,
// return the promise that already exists.
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 getting a TypeError: undefined is not a function
at the stadtMobilRates().success(function(data) {
line. Why is the stadtMobilRates
factory not accepted although I've injected it into the controller?
Edit #1: I've added the name of the factory to the array as suggested 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 null
?
Edit #2: I've created a simplified version of my app on Plunker. Well it works. On the full app I'm working with different routes, where stadtmobilRates
still remains null
. I'm unable to create a Plunker of the full app with the routes. So here is the full code on GitHub. The code above is from Line 159. I guess it has something to do with my routes?