如何正确扩展FormGroup

时间:2017-11-20 21:59:24

标签: typescript metadata angular-forms angular5

我目前正在尝试扩展Formular类型的Angular5,以实现一个自定义方法来接收模型状态错误并将它们显示到另一个自定义组件中。

不幸的是,我一直在收到一个不那么丰富的错误:
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

找到

1 个答案:

答案 0 :(得分:0)

最后,问题是我正在注册错误的服务。而不是在App.module而不是App.components上注册它 - 在提供者内部。一旦我改变它,问题就消失了。