2014-06-11 3 views
0

각 애플 리케이션을위한 블루투스 저에너지 서비스의 두 가지 구현이 있습니다.

따라서 bluetoothServiceProvider 일반을 작성하여 올바른 bluetooth 서비스 구현을 반환해야합니다. 하나는 cordovaBluetoothService이고 다른 하나는 chromeBluetoothService입니다. 그래서 window 속성을 확인하고 필요한 것을 결정하십시오.

나는 내가

this.$get = [ 
    'chromeBluetoothService', 
    'cordovaBluetoothService', 
    function(chromeBtS, cordovaBtS) { 
     if(window.cordova) { 
      return cordovaBtS; 
     else { 
      return chromeBtS; 
     } 
    } 
]; 

처럼 $get 함수로 두 서비스를 주입 할 수있어하지만 모두 종속성 주입에 인스턴스화 이후이 최적이 아닌 그래서 (내가 구현 내에서 특징 검출을 수행 할 그나마) 나는 그들이 if 절 안에서 인스턴스화되기를 원한다. 어떻게해야합니까?

var $injector = angular.injector(); 
return $injector.get('chromeBluetoothService'); 

을하지만 내가 할 경우 Unknown provider: chromeBluetoothServiceProvider <- chromeBluetoothService

을 반환합니다 :

나는 노력이이 설정 단계가 여전히 때문에 var $injector = angular.injector('myApp.services');

이 모듈을 인스턴스화 할 수 없습니다.

답변

1

앱의 $injector을 삽입해야 앱에서 사용할 수있는 서비스를 알 수 있습니다.
다행히 우리는 $injector 서비스 자체를 주입하는 방법을 알고 :

this.$get = ['$injector', function ($injector) { 
    if (window.cordova) { 
     return $injector.get('cordovaBluetoothService'); 
    else { 
     return $injector.get('chromeBluetoothService'); 
    } 
} 
관련 문제