I am working on Ionic Framework and there is a requirement that I need to inject the angular services in controller to get the data or perform any CRUD operation in backend. Please Let me know how I can do this?
Asked
Active
Viewed 1,283 times
-2
-
1http://stackoverflow.com/questions/16876748/service-injection-into-controller-with-angularjs look here – Mohan Gopi May 03 '16 at 10:40
-
Have you got any sample code or any attempts to show us? – Hopeful Llama May 03 '16 at 10:44
-
Thanks Mohan.. Its working for me.. – Nimesh.Nim May 03 '16 at 11:14
3 Answers
0
Ionic Framework
is build upon angular.js
, so there s no difference in doing this in Ionic
.
Its simple like
var myapp = angular.module('myApp',['ionic']);
myapp.service('MyService',['$http', function($http) {
return {
doSomeThing: function() {
// do something here with $http or whatever
},
doSomeThingElse: function() {
}
}
]);
myapp.controller('MyController',['MyService',function(MyService) {
MyService.doSomeThing();
}]);

M. Junaid Salaat
- 3,765
- 1
- 23
- 25
0
if you are using Ionic2 here is an example on how to inject a Service into a controller; note the @Injectable
decorator in the Service, the [providers]
parameter of the @Page
decorator in the Controller, and how the service instance is a parameter of the constructor
.

Community
- 1
- 1

Manu Valdés
- 2,343
- 1
- 19
- 28
0
In addition to the answer provided by @mJunaidSalaat,
- You need to inject IONIC module to angular app module
- The remaining implementation will be as it is in Angular
As per below, you can:
angular.module('myApp', ['ionic'])

Draken
- 3,134
- 13
- 34
- 54

vijay singh
- 71
- 4