I want to access an array which is held in another service file (chart-serv.js), from my service file return-serv.js. How do you call the dependent file which is in the double braces [ ], line 1?
return-serv.js
var app = angular.module('starter.return-serv', ['starter.chart-serv'])
app.factory("ReturnData", function() {
return {
allData: function() {
var data = starter.chart - serv.chartPricesUpTo6Hours;
return data
}
}
});
chart-serv.js
var app = angular.module('starter.chart-serv', [])
var chartPricesUpTo6Hours = {
"10": {
"1": 95,
"2": 125,
"3": 155,
"4": 185,
"5": 215,
"6": 245
},
"20": {
"1": 105,
"2": 135,
"3": 165,
"4": 195,
"5": 225,
"6": 255
}
};
The part I'm not sure of is the allData function data variable. What should you call to access the other file?
You can create a service in chartserv.js to pass an object to your factory in returnserv.js by injecting service into factory.
var app = angular.module('chartserv', []);
app.service('FromServiceToFactory', function(){
var chartPricesUpTo6Hours = {
"10": {
"1": 95,
"2": 125,
"3": 155,
"4": 185,
"5": 215,
"6": 245
},
"20": {
"1": 105,
"2": 135,
"3": 165,
"4": 195,
"5": 225,
"6": 255
}
};
return {chartPricesUpTo6Hours: chartPricesUpTo6Hours};
});
var app = angular.module('returnserv', []);
app.factory("ReturnData", function(FromServiceToFactory) {
return {
allData: function() {
var valueFromService = FromServiceToFactory.chartPricesUpTo6Hours;
return valueFromService;
}
}
});
if it is a java script file then you first need to load it in your index html like so
<script src="path/to/file/starter.chart.js">
then in your starter.chart.js you would have your module or attach it to another module
so angular.module('start.chart')
or if it is a controller angualr.module('module to attach to').controller('controller name')
Here is some code from a app I made to give you a general Idea
my app.module.js file
angular.module('ffApp', ['ionic', 'controllers', 'services', 'angulartics', 'angulartics.google.analytics', 'ngCordova', 'pdf', 'ngSanitize', 'ngImgCrop', 'chart.js']);
my app.controller.js file
angular.module('controllers', ['ionic', 'ngCordova', 'services', 'pdf', 'ngSanitize', 'ngImgCrop', 'chart.js']);
my home.controller.js file
angular.module('controllers').controller('home.controller',
function ($scope, ffService, $state, $http, $ionicHistory, $ionicSideMenuDelegate, $rootScope, $cordovaCamera, $ionicPlatform, $ionicLoading)
so when my app.moduels gets loaded it is dependent on my controllers, so it goes and gets the controllers module, each on of my controllers is attached to the controllers module so they all get loaded too, even though they are in separate files. Also dont forget to load it in your index.html