In my Angular 2 application, I am using an observable to keep track of an array of records.
As the results of this observable are stored in the variable "records", I am utilizing *ngFor="let record of records" to iterate over and display them in my view. Everything is functioning correctly as expected.
However, I am encountering an issue when trying to log the length of the array of records in a separate function, as it always returns "0".
Currently, I am performing these operations within the ngOnInit life cycle hook of Angular. Here is how it is implemented:
ngOnInit() {
this.clientService.getAllClients()
.subscribe(resRecordsData => this.records = resRecordsData,
responseRecordsError => this.errorMsg = responseRecordsError);
this.isActive();
}
Following the ngOnInit method, I have defined the isActive function like so:
isActive() {
console.log('The total number of active records is ' + this.records.length);
}
It seems that the problem may be arising because "records" is not available at the time of the console log during OnInit. Is there a different approach I should take to retrieve the actual length of the array once "records" has been returned?