只在可观察对象条目少于九个时显示 MatChips。

huangapple go评论59阅读模式
英文:

Dsiplay MatChips only if the observable has less then nine entries

问题

以下是翻译好的部分:

<mat-chip-list *ngIf="selectedAttributes$ | async as attributes">
  <div *ngFor="let attribute of attributes">
    <mat-chip *ngIf="attribute.isSelected">{{attribute.label}}
      <button matChipRemove
        (click)="onChipClose(attribute.label)">
        <mat-icon>cancel</mat-icon>
      </button>
    </mat-chip>
  </div>
</mat-chip-list>
selectedAttributes$: Observable<SearchSettings[]> = this.searchSettingsService.searchSettings$;
*ngIf="(selectedAttributes$ | async)?.length < 9"

请注意,我已将您提供的代码中的 HTML 和 TypeScript 部分进行了翻译,但没有进行其他额外的回答。

英文:

I have a mat-chip-list with an ngFor and the matchips shall only be displayed if the array of my observable has less then nine entries.

This is my HTML:

&lt;mat-chip-list *ngIf=&quot;selectedAttributes$ | async as attributes&quot;&gt;
  &lt;div *ngFor=&quot;let attribute of attributes&quot;&gt;
    &lt;mat-chip *ngIf=&quot;attribute.isSelected&quot;&gt;{{attribute.label}}
      &lt;button matChipRemove
        (click)=&quot;onChipClose(attribute.label)&quot;&gt;
        &lt;mat-icon&gt;cancel&lt;/mat-icon&gt;
      &lt;/button&gt;
    &lt;/mat-chip&gt;
  &lt;/div&gt;
&lt;/mat-chip-list&gt;

This is my Obserable:

 selectedAttributes$: Observable&lt;SearchSettings[]&gt; = this.searchSettingsService.searchSettings$;

I tried to add another div with an if condition like this:

*ngIf=&quot;(selectedAttributes$ |async)?.lenght &lt; 9&quot;

but it did not work out.

答案1

得分: 0

请看以下翻译:

<ng-container *ngIf="selectedAttributes$ | async as attributes">
  <mat-chip-list *ngIf="attributes.length < 9">
    <div *ngFor="let attribute of attributes">
      <mat-chip *ngIf="attribute.isSelected">{{attribute.label}}
        <button matChipRemove
          (click)="onChipClose(attribute.label)">
          <mat-icon>cancel</mat-icon>
        </button>
      </mat-chip>
    </div>
  </mat-chip-list>
</ng-container>
英文:

Try this:

&lt;ng-container *ngIf=&quot;selectedAttributes$ | async as attributes&quot;
 &lt;mat-chip-list *ngIf=&quot;attributes.length &lt; 9&quot;&gt;
  &lt;div *ngFor=&quot;let attribute of attributes&quot;&gt;
    &lt;mat-chip *ngIf=&quot;attribute.isSelected&quot;&gt;{{attribute.label}}
      &lt;button matChipRemove
        (click)=&quot;onChipClose(attribute.label)&quot;&gt;
        &lt;mat-icon&gt;cancel&lt;/mat-icon&gt;
      &lt;/button&gt;
    &lt;/mat-chip&gt;
  &lt;/div&gt;
 &lt;/mat-chip-list&gt;
&lt;/ng-container&gt;

huangapple
  • 本文由 发表于 2023年2月6日 18:35:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定