首页 > 解决方案 > 如何使用用户输入表单的数据创建弹出窗口?

问题描述

<!--Currently making web app that is a bunch of forms. 

我使用带有 Angular 7 的 Visual Studio 2017 ASP.NET Core 2.2 创建。当用户填写表单时,并非所有表单数据都是必需的。用户输入所需的表单信息后,我希望有一个按钮,该按钮会弹出一个包含所有用户输入数据和相应问题的弹出窗口。如果他们将表单上的文本框留空或输入 n/a,我不希望信息出现在弹出窗口中。我希望弹出窗口有一个按钮来复制将粘贴到另一个站点上的表单中的数据。

I have created all my forms and also know how to create a popup.

我唯一的问题是我不知道如何将用户输入的信息从表单填充/传输到弹出窗口中,并排除用户未填写的文本框。-->

<!-- this is th .html -->

<form [formGroup]="Form" (ngSubmit)="onSubmit()">

    <div class="form-row align-items-center">
        <div class="col-auto">
            <br />
//
            <div class="form-group">
                <label for="Select1">Select your...</label>
                <select class="form-control" formControlName="Select1">
                    <option>...</option>
                    <option>...</option>
                    <option>Other</option>

                </select>
            </div>

            <div class="form-group">
                <label for="Number">some info </label>
                <textarea class="form-control" formControlName="Number" rows="1" placeholder="Separate case numbers by comma"></textarea>
            </div>

            <div class="form-group">
                <label for="Purpose">Purpose of info: </label>
                <textarea class="form-control" formControlName="Purpose" rows="1"></textarea>
            </div>

            <div class="form-group">
                <label for="name">Name of info: </label>
                <textarea class="form-control" formControlName="name" rows="1"></textarea>
            </div>

            <


        </div>

    </div>

    <p>
        Form Value: {{ Form.value | json }}
    </p>
    <p>
        Form Status: {{ Form.status }}
    </p>

    <button type="submit" class="btn btn-default" [disabled]="!labForm.valid">Build Lab Request</button>

    <!-- Trigger the modal with a button -->
    <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Template</button>

    <!-- Modal -->
    <div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title text-left">Lab Access Template</h4>
                </div>
                <div class="modal-body">
                    <p>Want user entered form data to appear on popup.</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Copy Template</button>
                </div>
            </div>

        </div>
    </div>

</form>

<!--this is the .ts-->
import { Component } from '@angular/core';
import { FormBuilder, FormControl, Validator, FormGroup, Validators, FormArray } from '@angular/forms';


@Component({
    selector: 'app-',
    templateUrl: './.component.html',
    styleUrls: ['./.component.scss']
})
/**  component*/
export class Component {



    Form = new FormGroup({
        Select1: new FormControl(''),
        Number: new FormControl('', Validators.required),
        Purpose: new FormControl(''),
        name: new FormControl('', Validators.required),

    });



    onSubmit() {
        console.warn(this.labForm.value);
    }


    /** LabAccess ctor */
    constructor(private fb: FormBuilder) { }



}

标签: javascripthtmlcssangularasp.net-core

解决方案


您尝试做的事情非常简单,并且您已经准备好很多东西(例如反应形式)来实现它。

你的模态很好,但你可能应该为弹出窗口使用一个单独的组件,并将信息传递给它,它看起来像这样:

在您的调用模板中,包括一些模板,该模板将具有弹出窗口或模式或其他包含组件的模板,请参见下文:

<ng-template #formPopup>
    <form-popup
        [select1]="Form.get( 'Select1' ).value"
        [number]="Form.get( 'Number' ).value"
        [purpose]="Form.get( 'Purpose' ).value"
        [name]="Form.get( 'name' ).value">
    </form-popup>
</ng-template>

这将在创建组件时将这些值作为输入传递到您的组件中:

export class FormPopupComponent {
    @Input() select1 : any;
    @Input() number : number;
    @Input() purpose : string;
    @Input() name : string;

    // Do things with these inputs as needed in this component
}

正如您所指出的,单击时,您还必须打开对话框或弹出窗口或其他任何内容,此处对此进行了详细描述。但这里有一个快速总结 - 在您的原始包含表单模板的组件中,您需要对我们已经创建的弹出窗口的引用:

@ViewChild( 'formPopup' ) formPopupRef : TemplateRef<any>;

constructor(public dialog: MatDialog) {}

    openDialog(): void {
        const dialogRef = this.dialog.open( this.formPopupRef );
    }

推荐阅读