简体   繁体   English

Primeng Autocomplete - 不使用 Angular 中的反应形式修补值

[英]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 .我们需要用keyvalue对象修补字段。 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.

相关问题 以编程方式为 PrimeNG AutoComplete 以反应形式选择值 - Programmatically selecting value for PrimeNG AutoComplete in reactive form Angular反应形式:嵌套形式的设置/修补值 - Angular Reactive Form: Setting/Patching Value for Nested Forms 使用primeng表单控件和角度反应表单如何获得下拉列表的值 - Using primeng form controls and angular reactive forms how can I get the value of the dropdown 使用 PrimeNG 在反应形式上显示复选框 - Display check box on reactive form using PrimeNG Angular 2 - 使用反应形式将值分配给复选框 - Angular 2 - Assign value to checkbox using reactive form 使用Angular中的反应形式在数组内部修补数组 - Patching Array Inside An Array Using Reactive Forms in Angular 有没有办法为 angular 反应形式启用自动完成功能? - Is there a way to enable the autocomplete for angular reactive form? 如何在Angular 4反应形式的PrimeNg MultiSelect中默认设置选定值? - How to set by default selected values in PrimeNg MultiSelect with Angular 4 Reactive Form? Angular formControlName - 反应形式 - 将值设置为 primeNg p-dropdown - Angular formControlName - reactive form - Setting values to primeNg p-dropdown 反应形式的Angular PrimeNG下拉组件-初始值 - Angular PrimeNG dropdown component in reactive forms - initial value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM