src/app/book-list/book-list.component.ts
Component that displays an overview of all the books in either a list/grid view
selector | app-book-list |
templateUrl | ./book-list.component.html |
constructor(booksService: BooksService)
|
Defined in src/app/book-list/book-list.component.ts:35
|
Auto-initialize a private reference in this component to the BooksService instance provided by Angular's dependency injector
Parameters :
|
ngOnInit |
ngOnInit()
|
Defined in src/app/book-list/book-list.component.ts:50
|
OnInit lifecycle hook to fetch all the books right after Angular creates this component See: {@link BooksService#getBooks}
Returns :
void
|
switchToListView |
switchToListView()
|
Defined in src/app/book-list/book-list.component.ts:58
|
Sets the current view type to List
Returns :
void
|
switchToGridView |
switchToGridView()
|
Defined in src/app/book-list/book-list.component.ts:65
|
Sets the current view type to Grid
Returns :
void
|
books |
books: |
Type : []
|
Defined in src/app/book-list/book-list.component.ts:25
|
Array of all the books displayed in this component |
view |
view: |
Type : ViewType
|
Defined in src/app/book-list/book-list.component.ts:35
|
Current view type |
viewType |
viewType: |
Default value : ViewType
|
Defined in src/app/book-list/book-list.component.ts:31
|
Reference to the ViewType enum so that it can be used in the template See: https://stackoverflow.com/a/35835985, {@link ViewType} |
import { Component, OnInit } from "@angular/core";
import { BooksService } from "app/books.service";
import { Book } from "app/models/book.model";
/**
* Enum that defines all possible view types
*/
enum ViewType {
List,
Grid
}
/**
* Component that displays an overview of all the books in either a list/grid view
*/
@Component({
selector: "app-book-list",
templateUrl: "./book-list.component.html"
})
export class BookListComponent implements OnInit {
/**
* Array of all the books displayed in this component
*/
books: Book[];
/**
* Reference to the ViewType enum so that it can be used in the template
*
* See: https://stackoverflow.com/a/35835985, {@link ViewType}
*/
viewType = ViewType;
/**
* Current view type
*/
view: ViewType = ViewType.List;
/**
* Auto-initialize a private reference in this component
* to the BooksService instance provided by Angular's dependency injector
*
* @param {BooksService} booksService the BooksService instance that Angular will inject
*/
constructor(private booksService: BooksService) { }
/**
* OnInit lifecycle hook to fetch all the books right after Angular creates this component
*
* See: {@link BooksService#getBooks}
*/
ngOnInit() {
this.booksService.getBooks()
.subscribe(books => this.books = books);
}
/**
* Sets the current view type to _List_
*/
switchToListView() {
this.view = ViewType.List;
}
/**
* Sets the current view type to _Grid_
*/
switchToGridView() {
this.view = ViewType.Grid;
}
}
<!-- BOOTSTRAP'S PANELS, see here: http://getbootstrap.com/components/#panels -->
<div class="panel panel-default">
<div class="panel-heading">
<!-- BOOTSTRAP'S BUTTON GROUPS, see here: http://getbootstrap.com/components/#btn-groups -->
<div class="btn-group">
<!-- https://angular.io/guide/template-syntax#class-binding -->
<button type="button" class="btn btn-default" [class.active]="view === viewType.List"
(click)="switchToListView()">
<span class="glyphicon glyphicon-th-list"></span> List
</button>
<button type="button" class="btn btn-default" [class.active]="view === viewType.Grid"
(click)="switchToGridView()">
<span class="glyphicon glyphicon-th"></span> Grid
</button>
</div>
</div>
<div class="panel-body" [ngSwitch]="view">
<book-list-view
[books]="books"
*ngSwitchCase="viewType.List">
</book-list-view>
<book-grid-view
[books]="books"
*ngSwitchCase="viewType.Grid">
</book-grid-view>
</div>
</div>