我正在将项目从angular2 RC4迁移到RC6,我有一个需要Http
的自定义表单验证器。
在迁移之前,我将ReflectiveInjector
与HTTP_PROVIDERS
一起使用,但是对于RC6,由于HTTP_PROVIDERS
已弃用,不再存在,因此不再可行。
这是Validator中的静态方法:
static checkVat(control: FormControl) {
let checkVatUrl = "http://localhost:8080/checkvat";
let injector = ReflectiveInjector.resolveAndCreate([HTTP_PROVIDERS]);
let http = injector.get(Http);
let authHttp = new AuthHttp(new AuthConfig(), http);
if (control.value === "") {
return new Observable((obs: any) => {
obs.next(null);
obs.complete();
});
} else {
return authHttp.get(checkVatUrl + "/" + control.value)
.map((data: Response) => {
if (data.json().valid) {
return null;
} else {
let reason = "isNotValidVat";
return {[reason]: true};
}
})
.catch(function (e) {
return new Observable((obs: any) => {
obs.complete();
});
});
}
}
只用HTTP_PROVIDERS
取代HttpModule
不起作用,我在stackoverflow(NG2 RC5: HTTP_PROVIDERS is deprecated)上发现了类似的关于测试的问题,但唯一的答案是针对测试的。
如何为RC6手动“注入”Http
或HttpModule
,如果此自定义验证器还有其他或更好的解决方案,我也会对此开放。
提前致谢。
更新:
checkVat
方法是静态的,这就是为什么我必须使用ReflectiveInjector而不是像其他地方一样通过构造函数注入它。
自定义Validator的使用方式如下:
this.vatCtrl = new FormControl("", Validators.compose([Validators.pattern(this.vatService.vatPattern)]),VatValidator.checkVat);
UPDATE2: 在GüntherZöchbauer的回答的帮助下,我将代码改为如下,使其无需静态功能,无需手动注射:
验证者:
@Injectable()
导出类VatValidator {
constructor(private http: Http) {
}
checkVat(control: FormControl) {
let checkVatUrl = "http://localhost:8080/checkvat";
let authHttp = new AuthHttp(new AuthConfig(), this.http);
if (control.value === "") {
return new Observable((obs: any) => {
obs.next(null);
obs.complete();
});
} else {
return authHttp.get(checkVatUrl + "/" + control.value)
.map((data: Response) => {
if (data.json().valid) {
return null;
} else {
let reason = "isNotValidVat";
return {[reason]: true};
}
})
.catch(function (e) {
return new Observable((obs: any) => {
obs.complete();
});
});
}
}
}
在具有FormControl的组件中:
constructor(private vatValidator: VatValidator) {
this.vatCtrl = new FormControl("", Validators.compose([Validators.pattern(vatPattern)]), this.vatValidator.checkVat.bind(this.vatValidator));
}
答案 0 :(得分:12)
import { ReflectiveInjector } from '@angular/core';
import { Http, XHRBackend, ConnectionBackend, BrowserXhr, ResponseOptions, XSRFStrategy, BaseResponseOptions, CookieXSRFStrategy, RequestOptions, BaseRequestOptions } from '@angular/http';
class MyCookieXSRFStrategy extends CookieXSRFStrategy {}
...
let http = ReflectiveInjector.resolveAndCreate([
Http, BrowserXhr,
{ provide: ConnectionBackend, useClass: XHRBackend },
{ provide: ResponseOptions, useClass: BaseResponseOptions },
{ provide: XSRFStrategy, useClass: MyCookieXSRFStrategy },
{ provide: RequestOptions, useClass: BaseRequestOptions }
]).get(Http);
当然,你仍然需要包含HttpModule,享受!
答案 1 :(得分:2)
如果稍微更改验证器类,则不需要静态方法
@Injectable()
class PatternValidator {
constructor(private http:Http){}
// this is a method that returns a validator function
// configured with a pattern
pattern(pattern) {
return (control:Control) => {
this.http.get(...)
...
}
}
}
您可以像以下一样使用它:
Http
)constructor(private pattern:PatternValidator) {}
bind(pattern)
传递,以便.this
继续在验证程序函数this.vatCtrl = new FormControl("",
Validators.compose([
this.pattern(this.vatService.vatPattern).bind(this.pattern)
]), VatValidator.checkVat);
答案 2 :(得分:0)
在RC5之后,您可以做的是,
import { HttpModule} from '@angular/http';
@NgModule({
imports: [ BrowserModule,HttpModule ], //<------HttpModule
declarations: [ AppComponent],
providers: [service],
bootstrap: [ AppComponent ]
})
在服务或组件中,
import { Http, Response } from '@angular/http';
@Injectable()
export class service{
constructor(private http:Http){} //<----inject here
// use http here
}