私は私のplatform=browser
コード私は実行時に決定されてPlatform
値を、ベースの右のインスタンスを作成するために、依存性注入を使用するにはどうすればよいangular2プラットフォームに応じて `@Ijectable`の別のサブクラスを得るにはどうすればよいですか?
import { Injectable } from '@angular/core';
import { Platform } from 'ionic-angular';
import { File } from 'ionic-native';
declare var window;
@Injectable()
export class ImageSrcService {
constructor(protected platform: Platform){
if (platform.is('cordova')) {
// I want an instance of CordovaService
} else {
// I want an instance of BrowserService
}
}
getSrc(id: string) : Promise<string> {}
}
export class BrowserService extends ImageSrcService {
constructor(protected platform: Platform){
super(platform);
}
getSrc(id: string) : Promise<string> {
const uri = `http://example.com/images/${id}.jpg`;
return Promise.resolve(uri);
}
}
export class CordovaService extends ImageSrcService{
constructor(protected platform: Platform){
super(platform);
}
getSrc(id: string) : Promise<string> {
// File.checkFile('/images/', `${id}.jpg`)
const fullPath = `/images/${id}.jpg`;
return new Promise<string>((resolve, reject)=>{
window.resolveLocalFileSystemURL(
fullPath
, (fileEntry) => {
if (fileEntry.isFile)
return resolve(fileEntry.nativeURL);
reject("File not found");
});
});
}
}
別に私のplatform=cordova
コードを維持するために、このような何かをしたいですか?
サービスのインスタンスを2つ作成し、依存関係注入useFactory機能を使用して、注入するサービスを決定することができます。詳しくはこちらを参照してください:https://angular.io/docs/ts/latest/guide/dependency-injection.html – galvan