-2

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?

Aditya Singh
  • 14,688
  • 12
  • 42
  • 64
Nimesh.Nim
  • 388
  • 1
  • 9

3 Answers3

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,647
  • 1
  • 22
  • 24
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,255
  • 1
  • 18
  • 28
0

In addition to the answer provided by @mJunaidSalaat,

  1. You need to inject IONIC module to angular app module
  2. The remaining implementation will be as it is in Angular

As per below, you can:

angular.module('myApp', ['ionic']) 
Draken
  • 3,109
  • 13
  • 34
  • 52