Here is my Books component that contains books array that I tried to console log. It logs an undefined
but it shows correctly in DOM after using *ngFor
.
import { Component, OnInit } from '@angular/core';
import { BooksService } from '../shared/books.service';
import { ActivatedRoute, Params } from '@angular/router';
import { Book } from './book.model';
@Component({
selector: 'app-books',
templateUrl: './books.component.html',
styleUrls: ['./books.component.scss']
})
export class BooksComponent implements OnInit {
books: Book[];
filteredBooks: Book[];
id: string;
constructor(
private booksService: BooksService,
private route: ActivatedRoute
) {}
ngOnInit() {
this.booksService.getJson()
.subscribe(response => (
this.books = response.json().books)
);
console.log(this.books) //undefined;
this.route.params.subscribe((params: Params) => {
this.id = params['id'];
});
const filter = this.books.filter(book => book.author === this.id);
console.log(filter);
}
}