在下面的代码中,我试图在表单提交中提交3个值(rid,金额和原因)。
element(by.xpath("//a[@routerlink='/opportunities/list']")).click();
我正在检索组件上的值为:
<div class="row " *ngFor="let pendingrequest of pendingrequests" >
<form [formGroup]="pf" (ngSubmit)="onSubmit()">
<div class="col" >
<input type="hidden" formControlName="rid" name="requestid" value="{{pendingrequest.id}}" />
RNuo. : {{pendingrequest.id}}
</div>
<div class="row">
<div class="col">
<input type="number" formControlName="amount" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Amount">
</div><div class="col">
<input type="text" formControlName="reason" class="form-control" id="exampleInputPassword1" placeholder="Reason">
</div><div class="col">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
我正确地获得了“金额”和“原因”,但是没有获得rid值,这是表格中已知的。它不需要提交,这就是为什么我将输入类型保留为“ hidden”以便摆脱。
请让我知道如何在组件上获得此rid值。
答案 0 :(得分:0)
使用隐藏字段不是有角度的方法,因为您不需要它。 顺便说一下,这是一个可能有用的示例:
<form [formGroup]="pf" (ngSubmit)="onSubmit()">
<div class="col" >
<input type="hidden" formControlName="rid" name="rid" />
</div>
<div class="row">
<div class="col">
<input type="text" formControlName="amount" class="form-control" id="amount" aria-describedby="emailHelp" placeholder="Amount">
</div><div class="col">
<input type="text" formControlName="reason" class="form-control" id="reason" placeholder="Reason">
</div><div class="col">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
我只是使用表格组设置rid参数:
import { Component } from '@angular/core';
import { MessageService } from 'primeng/api';
import { FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [MessageService]
})
export class AppComponent {
name = 'Angular';
constructor(private messageService: MessageService) {
this.pf.patchValue({rid: 10});
}
pf = new FormGroup({
rid: new FormControl(''),
amount: new FormControl(''),
reason: new FormControl('')
});
onSubmit(){
console.warn(this.pf.value);
}
}
请参见Stackblitz
中的示例