2016-11-18 3 views
2

私はAngular 2の最終版を使用しています。 私は角2のバリデーターサービスを持っています。私は、HttpModuleを使用する非同期検証用の静的メソッドを作成しています。したがって、このコンテキストでは、Httpを注入して使用する方法を学び、バックエンドを呼び出すことができます。私は以下のような宣言を試みました: 静的http:http;角度2の最終サービスで静的メソッドの内部でHttpを注入または使用する方法はありますか?

ValidationService.http.get()のような静的メソッドを使用しようとしました しかし、それはget-undefinedのようなエラーを投げています。

誰かがこれにいくつかの光を投げることができますか?

答えて

1
@NgModule(...) 
class AppModule { 
    ngDoBootstrap(moduleRef) { 
    let injector = moduleRef.injector; 
    // assign injector somewhere to a static field 
    } 
} 

次に、あなたはしかし、静的メソッドを回避しようとしてください

someStaticMethod() { 
    let validationService = someStatic.injector.get(ValidationService); 
} 

のようにそれを使用することができます。彼らはAngular2sアーキテクチャに反対しています。

+0

おかげで、その後、私は思う:あなたのOtherStaticClassあなたが書くの静的メソッドのどこかに

@NgModule({ bootstrap: [ AppComponent ], // due this method ngDoBootstrap is never call so we will get injector in constructor. ... }) export class AppModule { constructor(public appRef: ApplicationRef, public appState: AppState, public injector: Injector) { SomeStaticClass.angularInjector = injector; } ... } 

そして、私は静的を変えるべきです。私はこれについてもっとGoogleにすべきだと思う。 – revathi

+0

あなたは解決策を詳しく述べることができますか?私は検証サービスで同様の問題を経験しており、このチュートリアル[https://coryrylan.com/blog/angular-form-b​​uilder-and-validation-management]に従ってフォームの検証を作成しています。サービスにhttpを注入すると、私は定義されません。私は静的関数にはなじみがなく、なぜそれがうまくいかないのかわからないのですか? – PBandJen

+0

検証サービスは静的メソッドにどのように関連していますか?あなたが達成しようとしていることを示すコードを追加する新しい質問を作成する方が良いかもしれません。 –

0

私はこのポスト(http://www.adonespitogo.com/articles/angular-2-extending-http-provider/)を拡張してHttpクラスを拡張し、Httpのシングルトンを返す静的メソッドを追加しました。

./services/http.service.ts

import { Injectable, ReflectiveInjector } from '@angular/core'; 
import { 
    BaseResponseOptions, 
    BaseRequestOptions, 
    BrowserXhr, 
    ConnectionBackend, 
    CookieXSRFStrategy, 
    Headers, 
    Http as HttpParent, 
    Request, 
    RequestOptions, 
    RequestOptionsArgs, 
    Response, 
    ResponseOptions, 
    XHRBackend, 
    XSRFStrategy 
} from '@angular/http'; 
import { Observable } from 'rxjs/Observable'; 

@Injectable() 
export class CustomHttp extends HttpParent { 

    public static getInstance() { 
     if (!(this._instance instanceof CustomHttp)) { 
      this._instance = ReflectiveInjector.resolveAndCreate([ 
       CustomHttp, BrowserXhr, XHRBackend, 
       { provide: ConnectionBackend, useClass: XHRBackend }, 
       { provide: ResponseOptions, useClass: BaseResponseOptions }, 
       { provide: XSRFStrategy, useFactory:() => { 
         return new CookieXSRFStrategy(); 
        } 
       }, 
       { provide: RequestOptions, useClass: BaseRequestOptions } 
      ]).get(CustomHttp); 
     } 
     return this._instance; 
    } 

    private static _instance: CustomHttp; 
} 

./app.module.ts

import { NgModule } from '@angular/core'; 
import { HttpModule, RequestOptions, XHRBackend } from '@angular/http'; 
import { CustomHttp } from './services/http.service'; 

@NgModule({ 
    bootstrap: [ 
     AppComponent 
    ], 
    declarations: [ 
     AppComponent 
    ], 
    imports: [ 
     HttpModule 
    ], 
    providers: [ 
     { 
      provide: CustomHttp, 
      useFactory: (backend: XHRBackend, options: RequestOptions) => { 
       return new CustomHttp(backend, options); 
      }, 
      deps: [XHRBackend, RequestOptions] 
     }; 
    ] 
}) 
export class AppModule {} 

./services/validation.service.ts

import CustomHttp from './http.service'; 

export class ValidationService { 
    static public doSomething() { 
     CustomHttp.getInstance().get('some/api/').subscribe(
      (response) => { 
       console.log(response); 
      }) 
    } 
} 

私はそれがあなたのために働くことを望む。

0

私はangular-starterフレームワークを使用して、私は中src/app/app.module.tsファイルにインジェクタへのアクセスを取得:

SomeStaticClass.angularInjector.get(ValidationService); 
関連する問題