[英]Programaticly fill and submit form Angular 4
I have following problem: I need to auto fill form and immediately submitted to some url with post method. 我有以下问题:我需要自动填写表格,并立即使用post方法提交到某些url。 this is Component .ts and html example:
这是Component .ts和html示例:
import { Component, OnInit, ViewChild, ElementRef, AfterViewInit, Renderer } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { PayModel } from './shared/pay.model'; import { PayService } from './shared/pay.service'; @Component({ selector: 'cp-pay', templateUrl: './pay.component.html', styleUrls: ['./pay.component.css'] }) export class PayComponent implements OnInit { model: PayModel; cardProcessingId: number; constructor( private payService: PayService, private route: ActivatedRoute, private renderer: Renderer ) { this.cardProcessingId = route.snapshot.params["processingid"]; } ngOnInit() { this.model = new PayModel(); this.getProcessing(); } getProcessing() { this.payService.getProcessing(this.cardProcessingId).subscribe( res => { this.model.fname = res.fname; this.model.lname = res.lname; this.model.age = res.age; } err => { console.log('getProcessing Error') }, () => { let form: HTMLFormElement = <HTMLFormElement>document.getElementById('payform'); form.submit(); } ); } }
<form ngNoForm id="payform" action="https://localhost:44300/api/pay/test" target="_blank" method="post"> <input type="text" id="fname" name="merchant" value="{{model.fname}}" /> <input type="text" name="lname" value="{{model.lame}}" /> <input type="text" name="age" value="{{model.age}}" /> <input type="submit"> </form>
I just want raw html form to be posted to url, that's all, but when code hits form.submit() it posts null values only. 我只希望将原始html表单发布到url,仅此而已,但是当代码命中form.submit()时,它仅发布空值。 I tried to simulate submit button click but it does not work either.
我试图模拟“提交”按钮单击,但是它也不起作用。 I wonder is that kind of thing even possible?
我不知道这种事情可能吗? It seems to me that form gets submitted before the form is filled.
在我看来,表格是在填写表格之前提交的。 Pls help
请帮助
Finally! 最后! I have found a workaround.
我找到了一种解决方法。 First things first: the problem was that form submit was happening before the html was populated with values and data posted was empty (null).
首先是第一件事:问题在于表单提交是在html填充值之前发生的,并且发布的数据为空(空)。 I didn't know that with Angular could force html to be populated with values pretty much whenever u want using ApplicationRef module.
我不知道,只要您想使用ApplicationRef模块,使用Angular就会迫使html几乎填充值。 All you need to do is to import module, create ApplicationRef instance and call it's method .tick() whenever you want to html values to be filled.
您需要做的就是导入模块,创建ApplicationRef实例,并在需要填充html值时调用它的方法.tick()。 My code should be like this now:
我的代码现在应该像这样:
import { Component, OnInit, ElementRef, AfterViewInit, ApplicationRef} from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { PayModel } from './shared/pay.model'; import { PayService } from './shared/pay.service'; @Component({ selector: 'cp-pay', templateUrl: './pay.component.html', styleUrls: ['./pay.component.css'] }) export class PayComponent implements OnInit { model: PayModel; cardProcessingId: number; constructor( private payService: PayService, private route: ActivatedRoute, private appRef: ApplicationRef ) { this.cardProcessingId = route.snapshot.params["processingid"]; } ngOnInit() { this.model = new PayModel(); this.getProcessing(); } getProcessing() { this.payService.getProcessing(this.cardProcessingId).subscribe( res => { this.model.fname = res.fname; this.model.lname = res.lname; this.model.age = res.age; appRef.tick(); // to immediately force html value fill } err => { console.log('getProcessing Error') }, () => { let form: HTMLFormElement = document.getElementById('payform'); form.submit(); } ); } }
On Html side ngNoForm not needed in form tag. 在HTML方面,form标记中不需要ngNoForm。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.