如何将提交的数字获取到模态窗口外的输入表单中。

huangapple go评论65阅读模式
英文:

How to get submitted number into the input form outside modal angular

问题

我试图创建一个包含按钮和手动输入数字的模态框,当点击提交按钮时,数字将显示在模态框外部的输入框中,并且可以用于诸如激活分钟数等的许多操作。我尝试了很多方法,但目前只能从console.log中获取数字,并且在按下按钮时防止重定向到?。当我手动输入数字时,数字为空。

如何制作一个好的模态框,可以将数字传递到模态框外部的输入框中?

<div class="flex flex-col w-full justify-center my-2" *ngIf="status === 'Preparing'">
    <button mat-raised-button (click)="openDialog('0ms', '0ms')">Input Target Time (按钮)</button>
    <form class="p-1" [formGroup]="timerForm">
        <div class="mb-2">目标时间(分钟)</div>
        <button (click)="clickButton(60)" class="bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2">60</button>
        <button (click)="clickButton(120)" class="bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2">120</button>
        <button (click)="clickButton(180)" class="bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2">180</button>
        <button (click)="clickButton(240)" class="bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2">240</button>
        <div>
            <input class="border-2 mt-3 w-full px-3 py-1 rounded-md text-black" formControlName="timer" [value]="timerForm.value.timer" type="number" placeholder="以分钟为单位的充电时间" />
        </div>
    </form>
</div>

<h1 mat-dialog-title>选择目标时间</h1>
<div mat-dialog-content>
    <button (click)="clickButton(60)" class="bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2">60</button>
    <button (click)="clickButton(120)" class="bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2">120</button>
    <button (click)="clickButton(180)" class="bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2">180</button>
    <button (click)="clickButton(240)" class="bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2">240</button>
    <form class="p-1" formGroup="timerForm">
        <div class="mb-2">目标时间(分钟)</div>
        <div>
            <input class="border-2 mt-3 w-full px-3 py-1 rounded-md" formControlName="timer" [value]="timerForm.value.timer" type="number" placeholder="以分钟为单位的充电时间" />
        </div>
    </form>
    <button type="submit" (click)="handleClose()" mat-dialog-close [disabled]="false" mat-button>提交</button>
</div>

import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormControl, FormGroup, NgForm } from '@angular/forms';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';

@Component({
  selector: 'dialog-animations-example-dialog',
  templateUrl: 'dialog-animations-example-dialog.html',
})
export class DialogAnimationsExampleDialog implements OnInit{
  public active = false;
  constructor(public dialogRef: MatDialogRef<DialogAnimationsExampleDialog>) {}

  public timerForm: FormGroup = new FormGroup({
    timer: new FormControl()
  });

  ngOnInit(): void {

  }

  @Output() cancel: EventEmitter<undefined> = new EventEmitter();

  public onCancel(e: PointerEvent): void {
    e.preventDefault();
    this.closeForm();
  }

  public closeForm(): void {
    this.active = false;
  }

  public clickButton(num: number) {
    this.timerForm.value.timer = num
    this.closeForm();
  }

  public handleClose(){
    console.log(this.timerForm.value.timer)
    this.dialogRef.close()
  }
}
英文:

I'm trying to make modal that have button and input manual number, so when get submit the number would be on the input outside the modal. and then can be use for many stuff like activate time minutes with the number and more. I try so many ways but I only can get the number from console.log for now and prevent from redirect to ? when i press the button. When I try input manual the number is null.

How to make a good modal that can get a number to the input outside the modal?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;div class=&quot;flex flex-col  w-full justify-center my-2&quot; *ngIf=&quot;status === &#39;Preparing&#39;&quot;&gt;
&lt;button mat-raised-button (click)=&quot;openDialog(&#39;0ms&#39;, &#39;0ms&#39;)&quot;&gt;Input Target Time ( Button )&lt;/button&gt;
&lt;form class=&quot;p-1&quot; [formGroup]=&quot;timerForm&quot;&gt;
&lt;div class=&quot;mb-2&quot;&gt;Target Time (minutes)&lt;/div&gt;
&lt;button
(click)=&quot;clickButton(60)&quot;
class=&quot;bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2&quot;
&gt;
60
&lt;/button&gt;
&lt;button
(click)=&quot;clickButton(120)&quot;
class=&quot;bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2&quot;
&gt;
120
&lt;/button&gt;
&lt;button
(click)=&quot;clickButton(180)&quot;
class=&quot;bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2&quot;
&gt;
180
&lt;/button&gt;
&lt;button
(click)=&quot;clickButton(240)&quot;
class=&quot;bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2&quot;
&gt;
240
&lt;/button&gt;
&lt;div&gt;
&lt;input
class=&quot;border-2 mt-3 w-full px-3 py-1 rounded-md text-black&quot;
formControlName=&quot;timer&quot;
[value]=&quot;timerForm.value.timer&quot;
type=&quot;number&quot;
placeholder=&quot;time charging on minutes&quot;
/&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;h1 mat-dialog-title&gt;Choose Target Time&lt;/h1&gt;
&lt;div mat-dialog-content&gt;
&lt;button
(click)=&quot;clickButton(60)&quot;
class=&quot;bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2&quot;
&gt;
60
&lt;/button&gt;
&lt;button
(click)=&quot;clickButton(120)&quot;
class=&quot;bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2&quot;
&gt;
120
&lt;/button&gt;
&lt;button
(click)=&quot;clickButton(180)&quot;
class=&quot;bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2&quot;
&gt;
180
&lt;/button&gt;
&lt;button
(click)=&quot;clickButton(240)&quot;
class=&quot;bg-green-400 hover:bg-green-300 cursor-pointer w-10 h-10 rounded-md mx-2&quot;
&gt;
240
&lt;/button&gt;
&lt;form class=&quot;p-1&quot; formGroup=&quot;timerForm&quot;&gt;
&lt;div class=&quot;mb-2&quot;&gt;Target Time (minutes)&lt;/div&gt;
&lt;div&gt;
&lt;input
class=&quot;border-2 mt-3 w-full px-3 py-1 rounded-md&quot;
formControlName=&quot;timer&quot;
[value]=&quot;timerForm.value.timer&quot;
type=&quot;number&quot;
placeholder=&quot;time charging on minutes&quot;
/&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;button type=&quot;submit&quot; (click)=&quot;handleClose()&quot; mat-dialog-close [disabled]=&quot;false&quot; mat-button&gt;Submit&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

import { Component, EventEmitter, Input, OnInit, Output } from &#39;@angular/core&#39;;
import { FormControl, FormGroup, NgForm } from &#39;@angular/forms&#39;;
import {MatDialog, MatDialogRef} from &#39;@angular/material/dialog&#39;;
@Component({
selector: &#39;dialog-animations-example-dialog&#39;,
templateUrl: &#39;dialog-animations-example-dialog.html&#39;,
})
export class DialogAnimationsExampleDialog implements OnInit{
public active = false;
constructor(public dialogRef: MatDialogRef&lt;DialogAnimationsExampleDialog&gt;) {}
public timerForm: FormGroup = new FormGroup({
timer: new FormControl()
});
ngOnInit(): void {
}
@Output() cancel: EventEmitter&lt;undefined&gt; = new EventEmitter();
public onCancel(e: PointerEvent): void {
e.preventDefault();
this.closeForm();
}
public closeForm(): void {
this.active = false;
}
public clickButton(num: number) {
this.timerForm.value.timer = num
this.closeForm();
}
public handleClose(){
console.log(this.timerForm.value.timer)
this.dialogRef.close()
}
}

<!-- end snippet -->

答案1

得分: 2

你可以从打开模态对话框时创建的modalRef中检索结果,订阅afterClosed()

const dialogRef = this.dialog.open(DialogAnimationsExampleDialog, {
  data: {inputValue: this.inputValue},
});

dialogRef.afterClosed().subscribe(result => {
  console.log('对话框已关闭,结果:', result);
});
英文:

You can retrieve the result from the modalRef you create when you open the modal, subscribing to afterCLosed():

    const dialogRef = this.dialog.open(DialogAnimationsExampleDialog, {
data: {inputValue: this.inputValue},
});
dialogRef.afterClosed().subscribe(result =&gt; {
console.log(&#39;The dialog was closed, result: &#39;, result);
});

huangapple
  • 本文由 发表于 2023年2月6日 15:37:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定