【发布时间】:2017-05-17 03:26:20
【问题描述】:
我正在开发 Angular/typescript 上的自定义验证器。这是我的课:
export class Validator {
constructor(private sharedDocument: SharedDocument) { }
static amountIsValid(control: FormControl): any {
return new Promise(resolve => {
setTimeout(() => {
console.log(Number.parseInt(control.value), "control.value");
if (Number.parseInt(control.value) >= this.sharedDocument.getNewRestToPay()) {
resolve({
"error !": true
});
}
else {
resolve(null);
}
}, 50);
});
}
}
如您所见,我尝试通过 DI 从另一个组件调用 getNewRestToPay() 方法。 但是这个组件只能是静态的。它告诉我一个错误: “Validator”类型上不存在属性“sharedDocument”。
如果我使用静态组件,我需要将组件端的方法更改为静态方法。在这种情况下,我无法归还我需要的财产。
@Injectable()
export class SharedDocument {
//some code
getNewRestToPay() :any{
return this.restCaisse;
}
}
我该如何解决这个问题?
【问题讨论】:
-
为什么不将
sharedDocument: SharedDocument作为参数传递给amountIsValid?
标签: angular typescript