Ionic. How inject ionicPlatform into the service?

2019-09-05 02:09发布

问题:

I'd like to inject $ionicPlatform abstraction into the service, but seemed I'm doing this wrong way. How should I use $ionicPlatform with service?

(function () {
    "use strict";
    angular.module('service', []).service('BBNService', ["$ionicPlatform", function ($http, $localStorage, $ionicPlatform) {

This code making $ionicPlatform undefined.

回答1:

Inject ionic dependency & Remove $http if you not using it as follows,

 angular.module('service', ['ionic'])
   .service('BBNService', ["$localStorage", "$ionicPlatform", 
       function ($localStorage, $ionicPlatform) {
   }]);


回答2:

It's undefined because you wrong the order. (use ionic.Platform)

Replace with this:

angular.module('service', [])
  .service('BBNService', ["$http", "$localStorage", 
     function ($http, $localStorage) {
       //use this
       var $ionicPlatform = ionic.Platform;

  }]);