How to get value of a FormControl in Angular4

前端 未结 3 1561
伪装坚强ぢ
伪装坚强ぢ 2021-02-19 06:18

I have some experience in Angular4, but I just inherited a piece of code that uses FormControls, and I don\'t know how to work with them. I am setting up a comments textarea th

相关标签:
3条回答
  • 2021-02-19 06:53

    Having validation at the FormGroup level in the component

    this.loginForm = new FormGroup({
      'email': new FormControl('example@example.com', Validators.compose([
        Validators.required,
        Validators.pattern('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$')
      ])),
    

    I was able to pluck the email value like this in my function:

    email = this.loginForm.value.email

    Worked for me - hope it helps you.

    0 讨论(0)
  • 2021-02-19 06:59

    Have a look for high level implementation and utilize few steps:

    StackBlitz URL: https://stackblitz.com/edit/angular-kqz4nw

    0 讨论(0)
  • 2021-02-19 07:10

    you can do this.rotationForm.get('comments').value this will give you the value of the formControl and then you can check on the length.

    doing this this.rotationForm.value.comments will work too BUT NOT for disabled fields, to get the value of the DISABLED fields use the this.rotationForm.get('comments').value

    0 讨论(0)
提交回复
热议问题