[英]Primeng Autocomplete - Not patching the value using reactive form in Angular
I have the latest version of Angular and using the autocomplete feature of Primeng and trying to patch the value after searching the match result using reactive form, but it is not reflecting , below is my code:我有最新版本的 Angular 并使用 Primeng 的自动完成功能并尝试在使用反应形式搜索匹配结果后修补该值,但它没有反映,下面是我的代码:
HTML Code: HTML代码:
<p-autoComplete formControlName="orderId" [suggestions]="orderList"
(completeMethod)="searchOrders($event)" (onSelect)="onOrderChange($event)" filter="label"
field="label" [autoHighlight]="true" [forceSelection]="true" dataKey="value">
</p-autoComplete>
TS Code:代码:
searchOrders(event) {
this._orderService.getOrderList(event.query).subscribe(
success => {
if (success['orders'].length) {
this.orderList = success['orders']
this.formGroup.patchValue({ orderId: { value: '6614876352770211840' } })
}
},
error => {
// handling errors here
}
)
}
I have an array of object ([{value:'', label: ''}])
in orderList
variable and list is properly showing there after search.我在
orderList
变量中有一个对象数组([{value:'', label: ''}])
并且列表在搜索后正确显示在那里。 onOrderChange
method is not impacting on this logic, I also separately called the searchOrders
method by passing the relative query and after that patch the value, I tried different ways, but it is not working. onOrderChange
方法不影响这个逻辑,我还通过传递相关查询单独调用了searchOrders
方法,在修补值之后,我尝试了不同的方法,但它不起作用。
Finally, got the solution, it was easy but unexpected.终于,得到了解决方案,很容易但出乎意料。 We need to patch the field with object of
key
and value
.我们需要用
key
和value
对象修补字段。 This is required for Autocomplete and Chips Components of Primeng to display the pre-defined values ({ value: '6614876352770211840', label: 'ORD00459' })
.这是 Primeeng 的 Autocomplete 和 Chips Components 显示预定义值所
({ value: '6614876352770211840', label: 'ORD00459' })
。
So the example is:所以例子是:
TS Code:代码:
searchOrders(event) {
this._orderService.getOrderList(event.query).subscribe(
success => {
if (success['orders'].length) {
this.orderList = success['orders']
this.formGroup.patchValue({ orderId: { value: '6614876352770211840', label: 'ORD00459' } })
}
},
error => {
// handling errors here
}
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.