我正在使用组件FormComponent构建一个应用程序.我正在使用角度核心的反应形式模块,并创建一个自定义验证器.该函数通过使用它来调用另一个函数 - 因为我认为它将引用FormComponent,但它引用的是'undefined'(?)
onInit中的代码定义FormGroup和FormControl,并在其外部定义函数
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.css']
})
export class FormComponent implements OnInit {
formInsurance:FormGroup;
private id:FormControl;
constructor(){}
ngOnInit() {
this.id = new FormControl('',[
Validators.required,
Validators.minLength(3),
Validators.maxLength(10),
Validators.pattern('^[0-9]+(-[0-9]+)+$|^[0-9]+$'),
this.foo
]);
this.formInsurance = new FormGroup({
id:this.id
})
}
foo(control:FormControl){
this.boo();
if(control.value){
return {
objToReturn: {
returned: name
}
};
} …Run Code Online (Sandbox Code Playgroud)