Как отключить кнопку в Angular, если список / массив не пустой? - PullRequest
0 голосов
/ 05 июля 2018

В моем компоненте я получаю данные из моего сервиса:

ngOnInit() {
  this._sharedService.
    getReceiptItem().takeUntil(this.ngUnsubscribe).
    subscribe(products => this.receiptItems = products);
}

Так как же отключить кнопку, если в моем массиве this.receiptItems есть какие-либо элементы?

Я пробовал что-то вроде этого:

 <div class="top-left">
    <button [disabled]="receiptItems.count>0" type="button" [routerLink]="['/administration']"><i class="fas fa-bars fa-fw"></i></button>
 </div>

Но, очевидно, это не решение ..

Спасибо

Ответы [ 4 ]

0 голосов
/ 05 июля 2018
<button *ngIf="receiptItems!=undefined" [disabled]="receiptItems.length>0" type="button" [routerLink]="['/administration']"><i class="fas fa-bars fa-fw"></i></button>
0 голосов
/ 05 июля 2018

Вам нужно Array.length в JavaScript

   <button [disabled]="receiptItems.length>0" type="button" [routerLink]="['/administration']"><i class="fas fa-bars fa-fw"></i></button>
0 голосов
/ 05 июля 2018

Обновите ваш код следующим образом:

<div class="top-left">
    <button [disabled]="receiptItems && receiptItems.length>0" type="button" [routerLink]="['/administration']"><i class="fas fa-bars fa-fw"></i></button>
 </div>
0 голосов
/ 05 июля 2018

Вы можете попробовать с length

component.ts

receiptItems:Array<any>;
ngOnInit() {
  this._sharedService.
    getReceiptItem().takeUntil(this.ngUnsubscribe).
    subscribe(products => this.receiptItems = products);
}

component.html

<div class="top-left">
    <button [disabled]="receiptItems.length > 0" type="button" [routerLink]="['/administration']"><i class="fas fa-bars fa-fw"></i></button>
 </div>
...