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?
-
1stackoverflow.com/questions/16876748/… look hereMohan Gopi– Mohan Gopi2016-05-03 10:40:09 +00:00Commented May 3, 2016 at 10:40
-
Have you got any sample code or any attempts to show us?Hopeful Llama– Hopeful Llama2016-05-03 10:44:17 +00:00Commented May 3, 2016 at 10:44
-
Thanks Mohan.. Its working for me..Nimesh.Nim– Nimesh.Nim2016-05-03 11:14:29 +00:00Commented May 3, 2016 at 11:14
Add a comment
|
3 Answers
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.
Comments
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();
}]);