如何正确扩展FormGroup
How to extend FormGroup correctly
我目前正在尝试扩展 Angular5 的 FormGroup 类型以实现自定义方法来接收模型状态错误并将它们显示到另一个自定义组件中。
不幸的是,我收到了一个信息量不大的错误:
Error: ServerValidatedForm/src/platform/svalidated/group.component.ts:30:1: Error encountered in metadata generated for exported symbol 'SVFormGroup':
ServerValidatedForm/src/platform/svalidated/group.component.ts:33:22: Metadata collected contains an error that will be reported at runtime: Expression form not supported.
我怀疑我在 TypeScripting 本身上遗漏了一些东西。很高兴注意到我已经清理了 node_modules,从项目中删除了所有元数据并再次转译,每次我遇到同样的错误。
我正在使用:
"@angular/animations": "5.0.1",
"@angular/cli": "^1.5.0",
"@angular/common": "5.0.1",
"@angular/compiler": "^5.0.1",
"@angular/compiler-cli": "^5.0.1",
"@angular/core": "5.0.1",
"@angular/forms": "^5.0.1"
重要代码在这里,任何帮助都会有帮助:
import { Injectable } from "@angular/core";
import { FormGroup } from "@angular/forms";
import { AbstractControlOptions } from "@angular/forms/src/model";
@Injectable()
export class SVFormGroup extends FormGroup {
constructor(
controls: {[key: string]: any},
validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null,
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null) {
super(controls, validatorOrOpts, asyncValidator);
}
setFromModelState(response: any, form: SVFormGroup) {
if(response.error && response.error.ModelState){
let validationErrorDictionary = response.error.ModelState;
for (var fieldName in validationErrorDictionary) {
if (validationErrorDictionary.hasOwnProperty(fieldName)) {
if (form.controls[fieldName]) {
form.controls[fieldName].setErrors(validationErrorDictionary[fieldName]);
} else {
this.setErrors(Object.assign(this.errors, validationErrorDictionary[fieldName]));
}
}
}
}
}
}
`
完整的代码可以在以下位置找到:https://github.com/eduelias/servervalidatedform
最后,问题是我注册服务有误。而不是在 App.module 而不是 App.components 上注册它 - 内部提供商。我一改,问题就消失了。
我目前正在尝试扩展 Angular5 的 FormGroup 类型以实现自定义方法来接收模型状态错误并将它们显示到另一个自定义组件中。
不幸的是,我收到了一个信息量不大的错误:
Error: ServerValidatedForm/src/platform/svalidated/group.component.ts:30:1: Error encountered in metadata generated for exported symbol 'SVFormGroup':
ServerValidatedForm/src/platform/svalidated/group.component.ts:33:22: Metadata collected contains an error that will be reported at runtime: Expression form not supported.
我怀疑我在 TypeScripting 本身上遗漏了一些东西。很高兴注意到我已经清理了 node_modules,从项目中删除了所有元数据并再次转译,每次我遇到同样的错误。
我正在使用:
"@angular/animations": "5.0.1",
"@angular/cli": "^1.5.0",
"@angular/common": "5.0.1",
"@angular/compiler": "^5.0.1",
"@angular/compiler-cli": "^5.0.1",
"@angular/core": "5.0.1",
"@angular/forms": "^5.0.1"
重要代码在这里,任何帮助都会有帮助:
import { Injectable } from "@angular/core";
import { FormGroup } from "@angular/forms";
import { AbstractControlOptions } from "@angular/forms/src/model";
@Injectable()
export class SVFormGroup extends FormGroup {
constructor(
controls: {[key: string]: any},
validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null,
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null) {
super(controls, validatorOrOpts, asyncValidator);
}
setFromModelState(response: any, form: SVFormGroup) {
if(response.error && response.error.ModelState){
let validationErrorDictionary = response.error.ModelState;
for (var fieldName in validationErrorDictionary) {
if (validationErrorDictionary.hasOwnProperty(fieldName)) {
if (form.controls[fieldName]) {
form.controls[fieldName].setErrors(validationErrorDictionary[fieldName]);
} else {
this.setErrors(Object.assign(this.errors, validationErrorDictionary[fieldName]));
}
}
}
}
} } `
完整的代码可以在以下位置找到:https://github.com/eduelias/servervalidatedform
最后,问题是我注册服务有误。而不是在 App.module 而不是 App.components 上注册它 - 内部提供商。我一改,问题就消失了。