Angular ngIf: Identifier 'length' is not defined

1.2k views Asked by At

My component has the property transfers$ that is an observable that can comes with an array or an object that contains the error message. I can only input [items] when transfers$ is Transfer[]. But even using condition

<app-checklist *ngIf="transfers?.length > 0" [items]="transfers"></app-checklist>

angular continues warning:

Identifier 'length' is not defined. 'Transfer[] | AppResponseError' does not contain such a member

Why is that?

Thank you

component.html

<ng-template #loadingTpl>loading</ng-template>

<ng-template #emptyList let-length="length">
    <p *ngIf="length === 0">Empty List</p>
</ng-template>

<ng-template #error let-title="title" let-message="message" let-code="code">
    <div *ngIf="code">
        Error: {{code}} , {{title}} , {{message}}
    </div>
</ng-template>

<div *ngIf="transfers$ | async; let transfers; else loadingTpl">
    <app-checklist *ngIf="transfers?.length > 0" [items]="transfers">
        <div *ngFor="let transfer of transfers">
            ..something here
        </div>
    </app-checklist>
    <ng-container *ngTemplateOutlet="emptyList; context: {length: transfers.length}"></ng-container>
    <ng-container *ngTemplateOutlet="error; context:transfers"></ng-container>
</div>

TransferService.ts:

import { Injectable } from '@angular/core';
import { of } from 'rxjs';

@Injectable()
export class TransfersService {
  transfers$ = of('real observable');
}

component.ts

import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { TransfersService } from './TransfersService';

export interface AppResponseError = {code:number,title:string,message:string};
export interface Transfer = {id:number,title:string};

@Component({
  selector: 'app-transfer',
})
export class TransferComponent implements OnInit {
  transfers$: Observable<Transfer[] | AppResponseError>;
  constructor(private transfersService: TransfersService) {
    this.transfers$ = this.transfersService.transfers$;
  }

  ngOnInit() {
    this.transfers$.subscribe((data) => {
      console.log(data);
    });
  }
}
1

There are 1 answers

2
Nilesh Patel On BEST ANSWER

You do not need additional *ngIf.

<div *ngIf="transfers$ | async; let transfers; else loadingTpl">

What this will do is wait until transfers$ | async has evaluated, and bind the result to the value of transfers (non-dollar-suffixed).

create below function in ts file.

isDataAvailable(arr){
 return Array.isArray(arr) && arr.length >0
}

and use it in template like this

<app-checklist *ngIf="isDataAvailable(transfers)" [items]="transfers"></app-checklist>
<div *ngIf="transfers$ | async; let transfers; else loadingTpl">
        <app-checklist *ngIf="isDataAvailable(transfers)" [items]="transfers"></app-checklist>
        <div *ngFor="let transfer of transfers">
            ..something here
        </div>
    <ng-container *ngTemplateOutlet="emptyList; context: {length: transfers.length}"></ng-container>
    <ng-container *ngTemplateOutlet="error; context:transfers"></ng-container>
</div>