[英]How do I create popup with data user enters into a form?
<!--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">×</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) { }
}
您嘗試做的事情非常簡單,而且您已經准備好很多事情(例如反應式表單)來實現它。
您擁有的模式很好,但是您可能應該對彈出窗口使用單獨的組件,並將信息傳遞給它,如下所示:
在您的調用模板中,包括一些模板,該模板將具有用於彈出窗口或模式窗口或包含組件的任何內容的模板,請參見下文:
<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 );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.