【问题标题】:Angular 5, Clarity Design - Select options "disappear" when pushing new item to options arrayAngular 5,Clarity Design - 将新项目推送到选项数组时选择选项“消失”
【发布时间】:2018-02-25 22:36:51
【问题描述】:

我正在开发一个使用 Clarity Design 作为 CSS 框架的 Angular 5 项目。

在一个选择上,我动态绑定了它的选项,当绑定一个新项目时发生了一件奇怪的事情。

基本上,新项目被添加到数据库中,在订阅回调中是完成绑定的地方。发生这种情况时,选择看起来好像没有选择任何选项,绑定正在正确进行,但视觉方面是我现在所坚持的。

以下是标记上的选择:

<div class="row">
    <div class="col-xs-12">
      <div
        class="select"
        [class.disabled]="contextResources.length < 1">
        <select
          [disabled]="contextResources.length < 1"
          (change)="emitSelectedResource(optionSelected)"
          [(ngModel)]="optionSelected">
          <option *ngIf="contextResources.length < 1">Agrega un {{ context.name | lowercase}} nuevo</option>
          <option
            *ngFor="let contextResource of contextResources"
            [ngValue]="contextResource">
          {{ contextResource.name }}</option>
        </select>
      </div>
    </div>
  </div>

以及组件方法:

addResource(): void {
    this.isLoading = true;
    this.resourceAdded = false;
    this.resourceError = false;

    let parentId = this.previousResource ? this.previousResource.id : null;

    this.resourceServices[this.currentStep].create({'newResource': this.newResource}, parentId)
      .finally(() => this.isLoading = false)
      .subscribe(
        response => {
          this.contextResources.push(response.json().newResource as ContextResource);

          if(this.contextResources.length == 1)
            this.emitSelectedResource(this.contextResources[0]);

          this.newResource = '';
          this.resourceAdded = true;
          this.emptyResources.emit(false);
        },
        (error: AppError) => {
          if(error instanceof BadRequestError)
            return this.resourceError = true;

          throw error; 
        }
      );
  }

这是选择某些选项时的样子,一切正常:

现在添加新项目后:

如果我们看一下列表内部:

有办法防止这种行为吗?

【问题讨论】:

    标签: angular select data-binding vmware-clarity


    【解决方案1】:

    是的,contextResources 每次都是全新的对象,并且 select 默认根据引用相等性检查选定的对象。

    答案是使用[compareWith] 输入,如下所述:https://angular.io/api/forms/SelectControlValueAccessor#caveat-option-selection

    <select [compareWith]="compareFn"  [(ngModel)]="selectedCountries">
        <option *ngFor="let country of countries" [ngValue]="country">
            {{country.name}}
        </option>
    </select>
    
    compareFn(c1: Country, c2: Country): boolean {
        return c1 && c2 ? c1.id === c2.id : c1 === c2;
    }
    

    【讨论】:

    • 我添加了您的答案中显示的内容,并将组件中的 compareFn 函数修改为“返回 c1.id === c2.id”,但它给了我以下错误“无法读取属性 'id '未定义的”。也许我需要让我的数组成为可观察的?
    • c1 &amp;&amp; c2 部分的全部意义在于检查undefined c1c2。如果您删除它,您将遇到您发布的确切错误。
    • 我尝试根据我看到的几个示例以这种方式实现它。这是有道理的,但我得到了同样的行为。我注意到的是,如果我手动选择其中一个选项然后添加另一个选项,则列表不再“消失”。仅当我在手动选择选项之前添加一个时它才会消失。
    • 好吧,我实际上通过编程选择默认值解决了最后一部分。它现在工作正常:)
    猜你喜欢
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 2016-08-20
    • 2020-04-10
    • 1970-01-01
    • 2014-01-22
    • 2015-03-22
    相关资源
    最近更新 更多