gpt4 book ai didi

javascript - 从单选按钮 Angular Material 对话框中获取值

转载 作者:行者123 更新时间:2023-11-30 09:17:59 25 4
gpt4 key购买 nike

我有 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>

所以当我点击单选按钮时,我需要得到点击单选按钮的值作为答案。

所以状态需要是单选按钮

我怎样才能正确地做到这一点?

最佳答案

当您使用响应式表单时,请在 mat-radio-group 上使用 formControlName,就像您在 matInput 字段上所做的那样。我已经 fork 并更新了你的 Stackblitz现在,当您单击保存按钮时,它也会在控制台上记录单选按钮值。您可以在支付组件中的任意位置获取和使用此值。

关于javascript - 从单选按钮 Angular Material 对话框中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53706217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com