Angular 6: 动态调用 FormArray 中的函数
Angular 6: Call a function inside an FormArray dynamically
<tr *ngFor="let bidRequestDetail of (bidRequestsById.bidRequestDetails || bidResponseForm.get('bidResponseDetails').controls); let i=index">
<td class="table-primary">
<div><small>{{i+1}}</small></div>
</td>
<td class="table-secondary">
<div><small>{{bidRequestDetail.product}}</small></div>
</td>
<td class="table-primary">
<div><small>{{bidRequestDetail.description}}</small></div>
</td>
<td class="table-secondary">
<div><small>{{bidRequestDetail.quantity}}({{bidRequestDetail.unit}})</small></div>
</td>
<td class="table-primary">
<div><small>
<i class="fas fa-rupee-sign"></i> {{bidRequestDetail.quotedAmount}}</small></div>
</td>
<td class="table`enter code here`-secondary">
<div class="form-group">
<form formArrayName="bidResponseDetails" (click)="addbidResponseDetails()">
<div [formGroupName]="i">
<div class="input-group" #myDiv>
<input type="number" formControlName="quotedAmount" class="form-control" placeholder="Amount {{i}}"
[ngClass]="{ 'is-invalid': submitted && bidRequestDetail.controls.quotedAmount.errors }" />
<input type="number" formControlName="quantity" class="form-control" placeholder="quantity"
[ngClass]="{ 'is-invalid': submitted && bidRequestDetail.controls.quantity.errors }" />
<textarea type="text" formControlName="notes" class="form-control" placeholder="notes"
[ngClass]="{ 'is-invalid': submitted && bidRequestDetail.controls.notes.errors }"></textarea>
</div>
</div>
</form>
</div>
</td>
</tr>
我需要在加载 div 标记时在格式数组中调用一个函数。
(单击)事件起作用,即在单击时创建一个数组。我需要动态触发函数调用。
您可以简单地在组件的 afterViewInit
生命周期挂钩中调用所需的函数,如下所示 -
class MyComponent implements AfterViewInit {
ngAfterViewInit() {
this.addbidResponseDetails()
}
}
我在这里使用 AfterViewInit
挂钩,因为它是
A callback method that is invoked immediately after Angular has completed initialization of a component's view. It is invoked only once when the view is instantiated.
<tr *ngFor="let bidRequestDetail of (bidRequestsById.bidRequestDetails || bidResponseForm.get('bidResponseDetails').controls); let i=index">
<td class="table-primary">
<div><small>{{i+1}}</small></div>
</td>
<td class="table-secondary">
<div><small>{{bidRequestDetail.product}}</small></div>
</td>
<td class="table-primary">
<div><small>{{bidRequestDetail.description}}</small></div>
</td>
<td class="table-secondary">
<div><small>{{bidRequestDetail.quantity}}({{bidRequestDetail.unit}})</small></div>
</td>
<td class="table-primary">
<div><small>
<i class="fas fa-rupee-sign"></i> {{bidRequestDetail.quotedAmount}}</small></div>
</td>
<td class="table`enter code here`-secondary">
<div class="form-group">
<form formArrayName="bidResponseDetails" (click)="addbidResponseDetails()">
<div [formGroupName]="i">
<div class="input-group" #myDiv>
<input type="number" formControlName="quotedAmount" class="form-control" placeholder="Amount {{i}}"
[ngClass]="{ 'is-invalid': submitted && bidRequestDetail.controls.quotedAmount.errors }" />
<input type="number" formControlName="quantity" class="form-control" placeholder="quantity"
[ngClass]="{ 'is-invalid': submitted && bidRequestDetail.controls.quantity.errors }" />
<textarea type="text" formControlName="notes" class="form-control" placeholder="notes"
[ngClass]="{ 'is-invalid': submitted && bidRequestDetail.controls.notes.errors }"></textarea>
</div>
</div>
</form>
</div>
</td>
</tr>
我需要在加载 div 标记时在格式数组中调用一个函数。 (单击)事件起作用,即在单击时创建一个数组。我需要动态触发函数调用。
您可以简单地在组件的 afterViewInit
生命周期挂钩中调用所需的函数,如下所示 -
class MyComponent implements AfterViewInit {
ngAfterViewInit() {
this.addbidResponseDetails()
}
}
我在这里使用 AfterViewInit
挂钩,因为它是
A callback method that is invoked immediately after Angular has completed initialization of a component's view. It is invoked only once when the view is instantiated.