从单选按钮“角度材质对话框”中获取价值

时间:2018-12-10 13:00:05

标签: javascript angular typescript angular-material

我有Angular Material对话框,在其中更新表格单击更改状态。

我需要从对话框中的单选按钮获取价值

这里是完整的示例

  

https://stackblitz.com/edit/angular-w9rbj1

这是组件的代码

   import {Component, Inject, OnInit, ViewEncapsulation} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef, MatDialog} from "@angular/material";
import {FormBuilder, Validators, FormGroup} from "@angular/forms";
import { Payment } from '../payments/payment';


@Component({
    selector: 'editing-dialog',
    templateUrl: './editing-dialog.component.html',
    styleUrls: ['./editing-dialog.component.scss']
})
export class EditingDialogComponent implements OnInit {
    form: FormGroup;
    reason:String;
    id: Number;
    statusdescription: String ;

    constructor(
        private fb: FormBuilder,
        private dialogRef: MatDialogRef<EditingDialogComponent>,
        @Inject(MAT_DIALOG_DATA) data:Payment) {
            this.reason = data.Reason;
            this.id  = data.Id;
            this.statusdescription = data.StatusDescription;
            this.form = fb.group({
                reason: [this.reason, Validators.required],
                id: this.id,
                status: this.statusdescription
            });
    }

    ngOnInit() {

    }
    save() {
        this.dialogRef.close(this.form.value);
    }

    close() {
        this.dialogRef.close();
    }
}

这是html

<h2>{{description}}</h2>

<mat-dialog-content [formGroup]="form">

    <mat-form-field>

        <input matInput min="0" max="100" required placeholder="Payment Reason" formControlName="reason" value="{{reason}}">

    </mat-form-field>
    <mat-radio-group>
        <mat-radio-button value="Approve">Approve</mat-radio-button>
        <mat-radio-button value="Reject">Reject</mat-radio-button>
      </mat-radio-group> </mat-dialog-content>

<mat-dialog-actions>

    <button class="mat-raised-button" (click)="save()">
        Ok
    </button>



    <button class="mat-raised-button"
            (click)="close()">
        Close
    </button>

</mat-dialog-actions>

因此,当我单击单选按钮时,我需要获取答案中单击的单选按钮的值。

所以状态必须是单选按钮

如何正确执行此操作?

2 个答案:

答案 0 :(得分:2)

在使用反应形式时,就像在formControlName字段上一样,在mat-radio-group上使用matInput。我已经分叉并更新了您的Stackblitz,当您单击“保存”按钮时,它现在也会在控制台上记录单选按钮的值。您可以在付款组件中的任意位置获取并使用此值。

答案 1 :(得分:0)

尝试一下;

@Input() 名称:字符串  <mat-radio-group name="status" ngModel #status="ngModel"> <mat-radio-button value="Approve">Approve</mat-radio-button> <mat-radio-button value="Reject">Reject</mat-radio-button> </mat-radio-group>