Angular 角度验证器不工作

Angular 角度验证器不工作,angular,angular-forms,Angular,Angular Forms,我正试图在我的表单中按照教程实现角度验证器,但是验证被忽略了。这是我的密码: 看法 我错过什么了吗?谢谢您的提示。您正在扰乱formControl的使用。创建自定义FormControl时,请使用FormControl的以下语法: // for single validator customControl = new FormControl('value', Validators.maxLength(5)); // for multiple validators(with Validators

我正试图在我的表单中按照教程实现角度验证器,但是验证被忽略了。这是我的密码:

看法

我错过什么了吗?谢谢您的提示。

您正在扰乱formControl的使用。创建自定义FormControl时,请使用FormControl的以下语法:

// for single validator
customControl = new FormControl('value', Validators.maxLength(5));

// for multiple validators(with Validators.compose)
customControl = new FormControl('value', Validators.compose([Validators.minLength(4), Validators.required]));
// or(without Validators.compose)
customControl = new FormControl('value', Validators.compose([Validators.minLength(4), Validators.required]));
将formGroup代码更改为以下任一种方式将解决您的问题:

选项1:在不使用新FormControl的情况下创建formGroup

选项2:更正formControl部件语法

this.imm = new FormGroup({
  comune: new FormControl(),
  provincia: new FormControl(),
  comuneObj: new FormControl(),
  cap: new FormControl(),
  indirizzo: new FormControl(),
  civico: new FormControl(),
  localita: new FormControl('', Validators.compose([Validators.minLength(4), Validators.required])),
  destinazioneUso: new FormControl(),
  destinazioneUsoAltro: new FormControl(),
  destinazioneUsoPrincipale: new FormControl(),
  destinazioneUsoSecondaria: new FormControl(),

});

支持该解决方案的进一步评论:我建议使用共享错误句柄组件或使用来自npm的外部包,例如避免在.html文件中进行许多不必要的错误处理
// for single validator
customControl = new FormControl('value', Validators.maxLength(5));

// for multiple validators(with Validators.compose)
customControl = new FormControl('value', Validators.compose([Validators.minLength(4), Validators.required]));
// or(without Validators.compose)
customControl = new FormControl('value', Validators.compose([Validators.minLength(4), Validators.required]));
this.imm = new FormGroup({
  comune: [],
  provincia: [],
  comuneObj: [],
  cap: [],
  indirizzo: [],
  civico: [],
  localita: ['', Validators.compose([Validators.minLength(4), Validators.required])],
  destinazioneUso: [],
  destinazioneUsoAltro: [],
  destinazioneUsoPrincipale: [],
  destinazioneUsoSecondaria: [],
});
this.imm = new FormGroup({
  comune: new FormControl(),
  provincia: new FormControl(),
  comuneObj: new FormControl(),
  cap: new FormControl(),
  indirizzo: new FormControl(),
  civico: new FormControl(),
  localita: new FormControl('', Validators.compose([Validators.minLength(4), Validators.required])),
  destinazioneUso: new FormControl(),
  destinazioneUsoAltro: new FormControl(),
  destinazioneUsoPrincipale: new FormControl(),
  destinazioneUsoSecondaria: new FormControl(),

});