繁体   English   中英

matInput 值未更新

[英]matInput value doesn't get updated

我有一个这样的用户名和密码的表单输入字段,

 <mat-form-field class="mdb-form-field-modal form-adjustments">
    <input (keydown)="emailBtnFocus($event)" tabindex="0" matInput placeholder="username" formControlName="username">
        <mat-error *ngIf="username.hasError('email') && !username.hasError('required')">
        {{ signInEmailErrorText }}
        </mat-error>
    <mat-error *ngIf="username.hasError('required')">
        {{requiredError }}
    </mat-error>
</mat-form-field>

export class SignInComponent implements OnInit {
    signInForm: FormGroup;
  username = new FormControl('', [
    Validators.required, Validators.email
  ]);

  ngOnInit() {
    this.authState = this.store.select('auth');
    this.signInForm = new FormGroup({
      'username': this.username,
      'password': this.password,
      'staySignedIn': this.staySignedIn
    });

  }
  // Submit Logic.
  onSignInSubmit() {
    console.log('SignInForm:', this.signInForm);
   //logic for submit goes here
  }
}

当我单击提交时,该值仍然保留为 null,我不知道为什么。 有人可以帮我解决这个问题吗?

做这个:

import { FormBuilder, FormGroup, Validators } from '@angular/forms';

export class SignInComponent implements OnInit {
signInForm: FormGroup;
constructor(private formBuilder: FormBuilder) {}

  ngOnInit() {
    this.authState = this.store.select('auth');
    this.signInForm = this.formBuilder.group({({
      'username': ['', [Validators.required, Validators.email]],
      'password': ['', Validators.required],
      'staySignedIn': ['']
    });

  }
  // Submit Logic.
  onSignInSubmit() {
    console.log('SignInForm:', this.signInForm);
   //logic for submit goes here
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM