web-dave / angular-starter-v2

6 stars 3 forks source link

Show Book Details #12

Open web-dave opened 7 years ago

web-dave commented 7 years ago
web-dave commented 7 years ago

generate

  ng g c books/book-details
web-dave commented 7 years ago

book-list.component.ts

 constructor(
    private booksService: BooksService, 
    private router: Router,
    private route: ActivatedRoute) { }

  selectBook(book: IBook) {
    this.router.navigate([book.isbn], {relativeTo: this.route});
  }
web-dave commented 7 years ago

books-routing.module.ts

  children: [{
      path: '',
      component: BookListComponent
    }, {
      path: ':isbn',
      component: BookDetailsComponent
    }]
web-dave commented 7 years ago

book-details.component.ts

  constructor(
      private booksService: BooksService,
      private router: Router,
      private route: ActivatedRoute) { }

  ngOnInit() {
    this.route
      .params
      .subscribe((params: {isbn: string}) => {
        this.booksService.getBook(params.isbn)
          .subscribe(b => {
            console.log('!!', b);
            this.book = b;
          });
      });
  }
web-dave commented 7 years ago

book-details.component.html (example)

<div class="panel panel-default" *ngIf="book" >
   <div class="panel-heading">{{book.title}} ({{book.isbn}})</div>
  <div class="panel-body">
    <p>{{book.abstract}}  <span class="badge">Seitenzahl: {{book.numPages}}</span> </p>
    <p>{{book.publisher.name}} ({{book.publisher.url}})</p>
  </div>
  <div class="panel-footer">{{book.author}}</div>
</div>