1

I have an Observable in my service and I want to display its size/length as part of my pagination.

public document: SomeClass[]

getDocument(): Observable<SomeClass[]> {
       return this._http.get(this.jsonURL)
            .map(this.extractData)
            .catch(this.handleError);
    }

<div *ngFor='let resultItem of document; let count=index'></div>

<pagination>{{document.length}}</pagination>

This is throwing an error: Cannot read property 'length' of undefined

How should it be done? thanks for your help.

arn-arn
  • 1,328
  • 1
  • 21
  • 31

2 Answers2

1

I was able to resolve it by putting *ngIf to check if document has value or not since it is an Observable and called Asynchronously. It means that data will come in later.

<pagination *ngIf='document'>{{document.length}}</pagination>
arn-arn
  • 1,328
  • 1
  • 21
  • 31
0

public document: SomeClass[]

document is not initialized, so there is no object which could have length attribute.

Initialize object, or use other variable in template, and update it.

private observableLegth: number = 0

and then

 observableLength = document.length

somewhere in your component class (when document is defined!)

jmachnik
  • 1,120
  • 9
  • 19
  • This throws me "Property length does not exist on type 'Observable'." This answer: https://stackoverflow.com/a/40237063/5326490 did worked for me in the .ts file, though you have to take the order of execution into consideration since the code executed inside the .subscribe() callback runs asynchronously. – Lual Oct 27 '17 at 19:23