【问题标题】:how to pass a value from a field in angular using form?如何使用表单以角度传递字段中的值?
【发布时间】:2021-09-14 07:50:44
【问题描述】:

单击按钮时,我想将具有 formControlName 电子邮件的输入字段的值传递给该函数,有人可以给我一个想法或帮助吗?谢谢。

#html代码

<div>
                    <div fxLayout="column">
                        <mat-form-field class="full-width" appearance="fill" style="margin-top: 20px;">
                            <mat-label>Username</mat-label>
                            <input matInput placeholder="" autocomplete="email" formControlName="email">
                            <mat-error *ngIf="modelForm.get('email').hasError('required')">
                                Username is required.
                            </mat-error>
                        </mat-form-field>
                    </div>
                    <div style="text-align: right;">
                        <button (click)="getStarted()" mat-flat-button color="primary"
                            type="submit" class="v-btn-sml" id="get-started-button">
                            Submit
                        </button>
                    </div>

#.ts

  getStarted() {
    // I want to pass the input value from the username field here when button is clicked
  }

  private Form(): FormGroup {
    return this.formBuilder.group({
      id: [this.model.id || 0],
      email: new FormControl(this.model.email, [Validators.required]),
    });
  }

【问题讨论】:

标签: angular typescript form-control


【解决方案1】:

假设您已将 modelForm 定义为组件上的属性,

getStarted(){
    const value = this.modelForm.value['email'];
}

【讨论】:

    【解决方案2】:

    你可以这样实现:

    getStarted() {
       const inputValue = this.modelForm.get('email').value
      }
    
     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 2018-07-13
      • 2021-10-17
      • 2017-08-21
      • 2016-02-02
      • 1970-01-01
      • 2018-09-03
      相关资源
      最近更新 更多