File

src/app/book-list/book-list.component.ts

Description

Component that displays an overview of all the books in either a list/grid view

Implements

OnInit

Metadata

selector app-book-list
templateUrl ./book-list.component.html

Constructor

constructor(booksService: BooksService)

Auto-initialize a private reference in this component to the BooksService instance provided by Angular's dependency injector

Parameters :
  • booksService : BooksService

    the BooksService instance that Angular will inject

Methods

ngOnInit
ngOnInit()

OnInit lifecycle hook to fetch all the books right after Angular creates this component

See: {@link BooksService#getBooks}

Returns : void
switchToListView
switchToListView()

Sets the current view type to List

Returns : void
switchToGridView
switchToGridView()

Sets the current view type to Grid

Returns : void

Properties

books
books: []
Type : []

Array of all the books displayed in this component

view
view: ViewType
Type : ViewType

Current view type

viewType
viewType:
Default value : ViewType

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>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""