typescript 角形材料React形式确认

jk9hmnmh  于 2022-11-26  发布在  TypeScript
关注(0)|答案(1)|浏览(118)

我想使用表单验证器,但我不知道具体如何使用,我试图同时设置max-length和required的验证,但我不能,而且我已经找过了,但什么也没找到
下面是我尝试做的事情:

import { literalMap } from '@angular/compiler';
import { Component, OnInit } from '@angular/core';
// import {FormsModule, ReactiveFormsModule} from '@angular/forms'
// import { FormControl } from '@angular/forms';
import { FormGroup, FormControl } from '@angular/forms';
import { Validators } from '@angular/forms';
@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.scss']
})
export class FormComponent implements OnInit {
  profileForm = new FormGroup({
    firstName: new FormControl('',Validators.maxLength(1), Validators.required),
    email: new FormControl('',Validators.email),
  });
  constructor() { }

  ngOnInit(): void {
  }
  onSubmit() {
    // TODO: Use EventEmitter with form value
    console.warn(this.profileForm.value);
  }
  testForm(){
    let a = this.profileForm.get('email').valid
    if (a){
      alert('jalo')
    }
  }
  save(){
    let email = this.profileForm.get('email').value
    if(email != '' ){
      alert('jalo')
    }
  }

}
thigvfpy

thigvfpy1#

将两个验证器像这样放在表中

profileForm = new FormGroup({
    firstName: new FormControl('',[Validators.maxLength(1), Validators.required] ),
    email: new FormControl('',Validators.email),
  });

相关问题