简体   繁体   English

无法在 p-autocomplete 中为 ngModel 设置值

[英]Can't set value to ngModel in p-autocomplete

I have edit component, where I use primeNG p-autocomplete我有编辑组件,我在其中使用 primeNG p-autocomplete

Here is html of this component这是此组件的 html

<div class="form-group row">
                <label class="col-lg-2 col-form-label" style="text-align:left;"><strong>Birim</strong></label>
                <div class="col-lg-5 ui-fluid">
                  <p-autoComplete [(ngModel)]="selectedBirimAdvanced" [suggestions]="filteredBirim" name="birim"
                                  (completeMethod)="search($event)"  [dropdown]="true" field="adi"
                                  (onSelect)="doOnSelect($event)"  >
                    <ng-template let-dto pTemplate="item">
                        <div>{{dto.adi}}</div>
                    </ng-template>
                  </p-autoComplete>
                  <span style="margin-left:50px">Birim: {{selectedBirimAdvanced||'none'}}</span>
                </div>
      
              </div>

Here is typescripts of this component这是这个组件的打字稿

@Component({
      selector: 'kt-talep-giris-edit',
      templateUrl: './talep-giris-edit.component.html',
      styleUrls: ['./talep-giris-edit.component.css'],
      providers: [ConfirmationService]
    })
export class TalepGirisEditComponent implements OnInit {
        constructor(...) { }

    talep: PerBirimTalep = new PerBirimTalep();
    /*dropdown variables*/
       birim2: any[];
       filteredBirim: PerBirim[]; 
       selectedBirimAdvanced: string;
    
    ngOnInit() {
    
        this.sub = this.route.params.subscribe(params => {
          const id = params['id'];    
          if (!isNaN(id) && id != 'new') {
            this.findByTalep(id);
          }
          this.getCombo();
        });
      }
    
         findByTalep(id: number) {
            
            this.perBirimTalepService.getByTalep(id).subscribe(
              data => {
                this.talep = data;
           
        },
          error => {
            //this.showError('HATA', error);
                this.showMessage('error', 'Kayıt Bulunamadı.', 'Uyarı');
              }
            );
          }
    //Birim autocomplete
    
      getComboBirim2() {
        this.parameterService.getBirim().subscribe(data => {
          this.birim2 = data;
        });
      }
    
      search(event) {
        let filtered: PerBirim[] = [];
        let query = event.query;
        for (let i = 0; i < this.birim2.length; i++) {
          let birim1 = this.birim2[i];
          if (birim1.adi.toLocaleLowerCase('tr').indexOf(query.toLocaleLowerCase('tr')) == 0) {
            filtered.push(birim1);
          }
        }
        this.filteredBirim = filtered;
      }
    
      doOnSelect(event) {
    
        this.selectedBirimAdvanced = event.birimId;
        console.log('selectedBirimAdvanced  '+ event.birimId )
      }
    
    
        }

My problem is p-autocomplete ngModel is not setted and "Birim" is null.我的问题是未设置 p-autocomplete ngModel 并且“Birim”是 null。 But I used combo-box ngModel is setted.但我使用组合框 ngModel 设置。 I wrote ngModel "selectedBirimAdvanced" in p-autocomplete.我在 p-autocomplete 中写了 ngModel "selectedBirimAdvanced"。

Here is browser screenshot.这是浏览器截图。 screenshot截屏

How can I set "Birim" field.如何设置“Birim”字段。 How can I fix it.我该如何解决。

I'm not sure if this is the problem, but maybe it helps: Try taking out the onSelect and see what happens.我不确定这是否是问题所在,但也许有帮助:尝试取出 onSelect 并看看会发生什么。 Your ngModel is 2 way bound, so why do the extra onSelect?你的 ngModel 是 2 路绑定的,那么为什么要额外的 onSelect 呢? Theoretically you are setting your model twice从理论上讲,您将 model 设置两次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM