TypeError: Cannot create property 'validator' on string 'abc@gmail.com' at setUpControl

后端 未结 3 1814
孤独总比滥情好
孤独总比滥情好 2021-02-12 01:41

I face issue in formGroup. First Based on URL I take some value and call to API for retrieve particular user-data for pre-field text.

register.html

3条回答
  •  灰色年华
    2021-02-12 01:57

    please try like this change [formControl] to formControlName.

    And to set the output to the input field please do the following, point the line this.form.patchValue

    import { Component } from '@angular/core';
    import { FormGroup, AbstractControl, FormBuilder, Validators } from '@angular/forms';
    import { Router, ActivatedRoute } from '@angular/router';
    import { EmailValidator, EqualPasswordsValidator } from '../../theme/validators';
    
    @Component({
      selector: 'register',
      templateUrl: './register.html',
    })
    export class Register {
      public form: FormGroup;
      public email: AbstractControl;
      public username: string;
    
      constructor(private registerService: RegisterService, fb: FormBuilder, private router: Router, private route: ActivatedRoute) {
        this.form = fb.group({
          'email': ['', Validators.compose([Validators.required])]
          .... etc..
        });
    
        this.email = this.form.controls['email'];
    
        this.registerService.getUser({ userId: "asdasd2123da2das" }).subscribe(posts => {
          if (posts) {
              var userObj = posts.json();
              console.log("userObj : ", userObj.data);
              if (userObj.data && userObj.data[0].email) {
                this.email = this.username = userObj.data[0].email;  // ouput : abc@gmail.com
                this.form.patchValue({
                    email : this.email
                 });
    
                this.isReadOnly = true;
                this.router.navigateByUrl('/register');
              } else {
                alert("You are Not Autorize to access this Page");
                this.router.navigateByUrl('/login');
              }
            }
        });
    

提交回复
热议问题