【问题标题】:Angular Material: Get value out of form [closed]Angular Material:从形式中获取价值[关闭]
【发布时间】:2019-03-25 19:10:26
【问题描述】:

我正在编写一个关于 Angular 的注册页面。两个单选按钮用于选择您是男性还是女性。此外,您还可以输入更多数据,例如姓名和地址。单击页面末尾的按钮应发送注册。

如何才能最好地获取 Typescript 文件中的值?

这是我要使用的组件:

InputSelectRadio ButtonDate Picker

https://material.angular.io/components/input/overview

https://material.angular.io/components/select/overview

https://material.angular.io/components/radio/overview

https://material.angular.io/components/datepicker/overview

【问题讨论】:

    标签: javascript angular forms angular-material


    【解决方案1】:

    你可以使用Angular's Reactive Forms,像这样:

    应用模块:

    import { ReactiveFormsModule } from '@angular/forms';
    
    @NgModule({
      imports: [
        // other imports ...
        ReactiveFormsModule
      ],
    })
    export class AppModule { }
    

    你的组件:

    import {FormControl, FormGroupDirective, FormGroup, NgForm, Validators} from '@angular/forms';
    ...
    export class SoknadsskjemaComponent implements OnInit {
    ...
    
      myForm = new FormGroup({
        gender: new FormControl('', [
          Validators.required
        ]),
        name: new FormControl('', [
          Validators.required,
          Validators.minLength(2)
        ]),
        birthdate: new FormControl('', [
          Validators.required,
        ]),
      });
    
        onSubmit() {
          // Do somthing
          // You can get the values in the forms like this: this.myForm.value
        }
    }
    

    你的组件 html

        <form autocomplete="on" [formGroup]="myForm" (ngSubmit)="onSubmit()">
            <mat-radio-group #rGroup class="radio-group" formControlName="gender">
                <mat-radio-button class="radio-button" value="female" radioGroup="rGroup">Female</mat-radio-button>
                <mat-radio-button class="radio-button" value="male" radioGroup="rGroup">Male</mat-radio-group>
            </mat-radio-group>
    
            <mat-form-field appearance="outline" class="full-width">
                <mat-label>Birthdate</mat-label>
                <input matInput [matDatepicker]="picker" formControlName="birthdate" autocomplete="bday">
                <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
                <mat-datepicker touchUi startView="multi-year" [startAt]="startDate" #picker></mat-datepicker>
    
              </mat-form-field>
    
              <mat-form-field appearance="outline" class="full-width">
                <mat-label>Name</mat-label>
                <input matInput type="text"  formControlName="name" name="given-name" autocomplete="given-name">
              </mat-form-field>
    
              <button mat-stroked-button type="submit" [disabled]="!myForm.valid" style="width:100%">Submitt</button>
         </form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多