简体   繁体   English

将用户输入的FORM数据发送到同一组件中的弹出窗口(不使用子/父组件或组件之间的组件)?

[英]Send User entered FORM data to a Popup within same component (without using child/parent components or component to component)?

Have a form where user enters answers to questions. 具有用户输入问题答案的表单。 I want all user entered data to appear in a popup along with the corresponding question. 我希望所有用户输入的数据与相应的问题一起出现在弹出窗口中。 If the user decides not to answer a question I do not want the question or anything associated with that question to appear. 如果用户决定不回答问题,则我不希望出现该问题或与该问题相关的任何内容。 I was told this could be done by converting entered data to a json object. 有人告诉我可以通过将输入的数据转换为json对象来完成。 I am not sure how to do this or if there is a better way. 我不确定如何执行此操作,或者是否有更好的方法。 I want to be able to do it all withing the same component. 我希望能够使用相同的组件完成所有操作。

I tried doing this How do I create popup with data user enters into a form? 我尝试执行此操作如何使用数据用户输入表单来创建弹出窗口? but could not figure it out. 但无法弄清楚。 I also want to be able to achieve this without creating more components. 我还希望能够在不创建更多组件的情况下实现这一目标。

//This is the .html file //这是.html文件

<div  ng-app="DemoApp" ng-controller="DemoController" class="form-row align-items-center">
    <div class="col-auto">
        <br />

        <div class="form-group">
            <label for="controlSelect1" >Select your tower...</label>
            <select class="form-control" name="exampleFormControlSelect1" formControlName="controlSelect1">
                <option></option>
                <option>Clown</option>
                <option>Clampionship</option>                  
                <option>Other</option>
            </select>
        </div>

        <div class="form-group">
            <label for="caseNumber">Case Number(s): </label>
            <textarea class="form-control" name="caseNumber" formControlName="caseNumber" rows="1" placeholder="Separate case numbers by comma"></textarea>
        </div>

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

        <label for="needRescources">Need any Lab Rescource(s)?</label>
        <br />
        <div class="form-check form-check-inline" name="labRescource" formControlName="labRescource">

            <input type="checkbox" class="form-check-input" formControlName="labAdmin">
            <label class="form-check-label" for="Lab Admin">Lab Admin&nbsp;&nbsp;</label>

            <input type="checkbox" class="form-check-input" formControlName="toolCart">
            <label class="form-check-label" for="Tool Cart">Tool Cart&nbsp;&nbsp;</label>

            <input type="checkbox" class="form-check-input" formControlName="other">
            <label class="form-check-label" for="Other">Other</label>

        </div>
        <br /><br />
        <div class="form-group">
            <label for="additionalNotes">Any additional notes? </label>
            <textarea class="form-control" name="additionalNotes" formControlName="additionalNotes" rows="1"></textarea>
        </div>


    </div>

</div>

<p>
    Form Value: {{ labForm.value | json }}
</p>
<p>
    Form Status: {{ labForm.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">
                <label for="exampleFormControlSelect1">Requester's Tower:</label>
                <br /><label for="caseNumber">Case Number(s): </label>
                <br /><label for="visitPurpose">Purpose of Lab Visit: </label>                   
                <br /><label for="needRescources">Need any Lab Rescource(s)?</label>


                {{ labForm.value | json }}
         </div>

            <div class="modal-footer">

                <button type="button" class="btn btn-default" data-dismiss="modal">Copy Template</button>

            </div>
        </div>

    </div>
</div>

//This is the .ts file //这是.ts文件

import { Component } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from'@angular/forms';


@Component({
selector: 'app-lab-access',
templateUrl: './lab-access.component.html',
styleUrls: ['./lab-access.component.scss']

}) })

/** LabAccess component*/
export class LabAccessComponent {


labForm = new FormGroup({
    ControlSelect1: new FormControl(''),
    caseNumber: new FormControl('', Validators.required),
    visitPurpose: new FormControl('', Validators.required),


    /**lab rescource check boxes*/
    labAdmin: new FormControl(''),
    toolCart: new FormControl(''),        
    other: new FormControl(''),

});



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

labAccessForm: FormGroup;

/** LabAccess ctor */
constructor() {  }   

} }

The form data is not appearing on popup in the format I desire. 表单数据没有以我想要的格式出现在弹出窗口中。 Would like it to look like below. 希望它看起来像下面。

Requestor's Tower: Clown 请求者塔:小丑

Case Number: 328792892 案例编号:328792892

Purpose of Visit: Felt like dropping by. 参观目的:觉得自己像过去。

Needed Resources: Tool Cart 所需资源:工具车

If user did not fill out one of the questions I do not want question appear. 如果用户未填写问题之一,则出现我不想要的问题。

This is how I decided to answer the question. 这就是我决定回答这个问题的方式。 I have a modal popup and the output is generated using this code. 我有一个模式弹出窗口,输出是使用此代码生成的。

<div *ngIf="form.value.Num1">Part Number(s): {{form.value.Num1}}</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM