我正在尝试在静态方法中使用注入的依赖项,当然注入的依赖项是实例范围并且不能在静态方法中使用。
这是我的课:
@Injectable()
export class PasswordValidationService {
constructor(private userAccountService:UserAccountService) {
}
static passwordValidator(control:AbstractControl) {
return control
.valueChanges
.debounceTime(400)
.switchMap(()=> this.userAccountService.checkCurrentPassword(control.value))
.map(res=> {
if (res.json() === true) {
return null;
}
else {
return {invalid: true};
}
}
);
}
}
我的问题是在静态方法中使用
UserAccountService
(依赖项)的最佳实践是什么?
编辑:我重新设计了我的应用程序,使用实例方法而不是静态方法,如下所示:
这是验证器:
import {Injectable} from "@angular/core";
import {UserAccountService} from "../../useraccount/useraccount.service";
import {AbstractControl} from "@angular/common";
import {Observable} from "rxjs/Observable";
@Injectable()
export class PasswordValidationService {
constructor(private userAccountService:UserAccountService) {
}
passwordValidator(control:AbstractControl):Observable<any> {
let validationResult = this.userAccountService.checkCurrentPassword(control.value)
.map(res=> {
if (res.json() === true) {
return null;
}
else {
return {invalid: true};
}
}
);
return validationResult;
}
}
这是使用验证器的组件:
constructor(private router:Router,
private formBuilder:FormBuilder,
private stylingService:StylingService,
private sessionService:SessionService,
private passwordValidationService:PasswordValidationService) {
}
ngOnInit() {
this.signinForm = this.formBuilder.group({
credentials: this.formBuilder.group({
username: [this.credentials.username, Validators.required],
password: [this.credentials.password, [Validators.required, this.passwordValidationService.passwordValidator]]
})
});
}
这是我收到的错误消息:
browser_adapter.ts:82 TypeError: Cannot read property 'userAccountService' of undefined
at PasswordValidationService.passwordValidator (http://localhost:8080/app/shared/services/password-validation.service.js:18:36)
at eval (http://localhost:8080/vendor/@angular/forms/src/validators.js:137:49)
at Array.map (native)
at _executeValidators (http://localhost:8080/vendor/@angular/forms/src/validators.js:137:23)
at FormControl.eval [as validator] (http://localhost:8080/vendor/@angular/forms/src/validators.js:116:33)
at FormControl.AbstractControl._runValidator (http://localhost:8080/vendor/@angular/forms/src/model.js:178:56)
at FormControl.AbstractControl.updateValueAndValidity (http://localhost:8080/vendor/@angular/forms/src/model.js:164:29)
at new FormControl (http://localhost:8080/vendor/@angular/forms/src/model.js:304:14)
at FormBuilder.control (http://localhost:8080/vendor/@angular/forms/src/form_builder.js:36:16)
at FormBuilder._createControl (http://localhost:8080/vendor/@angular/forms/src/form_builder.js:68:25)
要么不要使方法静态,要么从实例方法转发
export class PasswordValidationService {
constructor(private userAccountService:UserAccountService) {
}
validate(control:AbstractControl) {
return PasswordValidationService.passwordValidator(control);
}
static passwordValidator(control:AbstractControl) {
...
}
}
我使用它在应用程序初始化的某个地方将实例设置为静态,然后使用它。
class AppComponent {
constructor(private barService: BarService{
BarService.appInstance = barService
}
}
class FooUtil{
doSomethingFancy(){
//you might even check for BarService.appInstance the way you want to handle it
BarService.appInstance.somethingEvenMoreFancy()
}
}