I am still relatively new to Ionic and AngularJS and I have the following problem: I created a service in the controller.js
file and now need this service at loadup (app.js
) to detect which start page I will route to. How can I export the service from controller.js
into some other js file so that it will become available in both files app.js
and controller.js
.
Thanks,
EL
You import it using angular's dependency injection.
If your controller.js looks something like:
angular.module('myModule', [])
.service('myService', ['$scope', 'otherDependency', function($scope, otherDependency){
//some service code
}]);
Then the module you want to use the service in would need to import the module where the service is located and then inject the service itself. So something along the lines of:
angular.module('app', ['myModule'])
.controller('appCtrl', ['$scope', 'myService', function($scope, myService){
//some code using your service
}]);
This documentation might help: AngularJS services - https://docs.angularjs.org/guide/services AngularJS dependency injection - https://docs.angularjs.org/guide/di