如何使用数据用户输入表单来创建弹出窗口?

时间:2019-07-11 14:55:05

标签: javascript html css angular asp.net-core

<!--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) { }



}

1 个答案:

答案 0 :(得分:0)

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

您拥有的模态很好,但是您可能应该对弹出窗口使用单独的组件,并将信息传递给它,如下所示:

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

<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
}

正如您所指出的那样,单击时还必须打开对话框或弹出窗口,或任何in detail here所述的内容。但这是一个简短的摘要-在您的原始表单模板的组件中,您需要引用我们已经创建的弹出窗口:

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

constructor(public dialog: MatDialog) {}

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