【问题标题】:Cannot bind ngModel to p-dropdown value (Angular)无法将 ngModel 绑定到 p-dropdown 值(Angular)
【发布时间】:2019-10-12 18:16:20
【问题描述】:

我使用 PrimeNg 下拉菜单

这是模板的html

<div>
  <label>{{ l('Portfolio') }}</label>
  <p-dropdown
    [(ngModel)]="property.portfolioId"
    [disabled]="!landlordPortfolios.length"
    [options]="landlordPortfolios"
    autoWidth="false"
    [style]="{ width: '100%' }"
    name="landlordPortfolio"
    [autoWidth]="true"
  ></p-dropdown>
</div>

我通过此方法获取下拉列表的值

getLandlordPortfoliosById(landlordId: number): void {
  this.landlordPortfolios = [];

  this._landlordPortfolios.getPortfolioDropdownValuesById(landlordId).subscribe(result => {
    result.items.forEach(value => {
      this.landlordPortfolios.push({
        label: value.name,
        value: value.id,
      });
    });
  });
}

然后这样称呼它

if (this.property.landlordId) {
  this.getLandlordPortfoliosById(this.property.landlordId);

  this.initLandlordSuggestionsById(this.property.landlordId);
}

例如,我有landlordId = 1,并且选择的下拉选项也必须是id = 1。 这是结果

但是我在下拉列表中选择了项目,只是空白字段,单击下拉列表时我会看到所有选项。我的问题可能出在哪里?

【问题讨论】:

  • 你导入了FormsModule
  • 我不使用表单模块,我使用primeng的东西@SachinGupta
  • FormsModule 与此错误有何关联? @SachinGupta
  • 变量result长什么样子?
  • [(ngModel)]FormsModule 相关

标签: javascript angular typescript primeng


【解决方案1】:

所以问题出在数据请求/获取中

如果我将*ngIf 设置为下拉菜单,像这样*ngIf = "landlordPortfolios.length" 并删除[disabled],一切顺利。

【讨论】:

    猜你喜欢
    • 2017-12-06
    • 2021-06-24
    • 2021-05-23
    • 2018-03-20
    • 2022-08-18
    • 2018-12-05
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多