Closed. This question needs to be more focused 。它目前不接受答案。












想改善这个问题吗?更新问题,使其仅通过 editing this post 关注一个问题。

2年前关闭。



Improve this question




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

我怎样才能做到最好,在我的 Typescript 文件中获取值?

以下是我想使用的组件:
Input , Select , Radio 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

最佳答案

您可以使用 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>

关于javascript - Angular Material : Get value out of form,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52916984/

10-14 13:02
查看更多