代码之家  ›  专栏  ›  技术社区  ›  BBaysinger alex

为什么在formgroup.reset()之后formgroup控件为空?

  •  0
  • BBaysinger alex  · 技术社区  · 6 年前

    我有一个反应式表单,我正在设置:

    const v = Validators;
    
    this.entryForm = this.formBuilder.group({
    
      firstName: ['', [v.minLength(2), v.required]],
      lastName: ['', [v.minLength(2), v.required]],
      email: ['', [v.email, v.required]],
      instagram: ['', [v.minLength(2), v.required]],
      company: [''],
      title: [''],
      agree: [false, v.requiredTrue],
    
    });
    

    但在我打电话之后 this.entryForm.reset() ,例如:

    this.entryForm.reset();
    

    …的表单控件 entryForm null ,导致错误。即使我尝试重新实例化 FormGroup ,我还是明白了 无效的 表单控件我到底做错了什么?

    下面是完整的组件类型脚本:

    @Component({
      selector: 's2es-enter-page',
      templateUrl: './enter-page.component.html',
      styleUrls: [
        './enter-page.component.sass',
      ]
    })
    export class EnterPageComponent extends AbstractPageComponent implements OnInit {
    
      entryForm: FormGroup;
      inited = false;
    
      constructor(
        private readonly pageService: PageService,
        private readonly http: HttpClient,
        private readonly formBuilder: FormBuilder,
      ) {
        super(pageService);
    
        this.entryForm = new FormGroup({
          first: new FormControl(),
        });
    
        (window as any).localJsonpCallback = () => {
          this.entryForm.reset();
        };
      }
    
      onSubmit() {
        const url = 'https://script.google.com/macros/s/AKfycbyRrxv8Ri-GRpuXqWXo2inCPzmAE8mG6Q8oQIGPmUeMaGbD5jCn/exec?';
        const q: string[] = [];
        const company = this.entryForm.get('company').value + ',' + this.entryForm.get('title').value;
        const name = this.entryForm.get('firstName').value + ',' + this.entryForm.get('lastName').value;
    
        q.push('name=' + name);
        q.push('email=' + this.entryForm.get('email').value);
        q.push('instagram=' + this.entryForm.get('instagram').value);
        if (company.length > 1) { q.push('company=' + company); }
        q.push('prefix=localJsonpCallback');
    
        const uri = url + q.join('&');
        this.http.jsonp(uri, 'JSONP_CALLBACK').subscribe((val) => {
          console.log(val);
        });
      }
    
      /**
       * Since this is not supported on MS browsers.
       */
      placeholderShown(id: string): boolean {
        if (this.inited) {
          if (this.entryForm.get(id).value !== null) {
            return !this.entryForm.get(id).value.length;
          } else {
            // I don't know why this is null.
            // throw new Error(id);
            // return false;
          }
        }
      }
    
      setupForm() {
    
        const v = Validators;
    
        this.entryForm = this.formBuilder.group({
    
          firstName: ['', [v.minLength(2), v.required]],
          lastName: ['', [v.minLength(2), v.required]],
          email: ['', [v.email, v.required]],
          instagram: ['', [v.minLength(2), v.required]],
          company: [''],
          title: [''],
          agree: [false, v.requiredTrue],
    
        });
      }
    
      /**
       *
       */
      ngOnInit() {
    
        super.ngOnInit();
    
        this.setupForm();
    
        this.inited = true;
    
      }
    
    }
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   Tomas    6 年前

    官方文件( See here )事实上,它控制着 设置为空。不过,表单创建后不需要重置它如果您希望用其他值填充它(例如 '' ),你可以打电话 reset 具有 formState 参数设置为所需值。

        2
  •  -1
  •   Krishnavadan Raval    6 年前

    reset();用于在启动时将其重置为默认状态。 reset()用于清除窗体。

    因此,如果希望从中获取值,可以在ngSubmit=“saveForm()”上使用

    在saveForm方法中,可以获取值

    this.entryForm.controls.firstName.value

    希望对你有帮助。