Guest User

Untitled

a guest
Oct 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. import { BrowserModule } from '@angular/platform-browser';
  2. import { NgModule } from '@angular/core';
  3. import { FormsModule } from '@angular/forms';
  4. import { Routes, RouterModule } from '@angular/router';
  5.  
  6. import { AppComponent } from './app.component';
  7. import { FormInputComponent } from './form-input/form-input.component';
  8. import { MyBookService } from './mybook.service';
  9. import { HeaderComponent } from './header/header.component';
  10. import { FavsComponent } from './favs/favs.component';
  11. import { HomeComponent } from './home/home.component';
  12. import { FavouriteService } from './favs.service';
  13.  
  14. const appRoutes: Routes = [
  15. { path: '', redirectTo: '/home', pathMatch: 'full' },
  16. {path: 'home', component: HomeComponent},
  17. {path: 'favourites', component: FavsComponent},
  18. ];
  19.  
  20. @NgModule({
  21. declarations: [
  22. AppComponent,
  23. FormInputComponent,
  24. HeaderComponent,
  25. FavsComponent,
  26. HomeComponent
  27. ],
  28. imports: [
  29. BrowserModule,
  30. FormsModule,
  31. RouterModule.forRoot(appRoutes)
  32. ],
  33. providers: [MyBookService, FavouriteService],
  34. bootstrap: [AppComponent]
  35. })
  36. export class AppModule { }
  37.  
  38. <!-- loading form -->
  39. <app-form-input></app-form-input>
  40.  
  41. <!-- output -->
  42. <div class="card text-white bg-dark" style="max-width: 18rem;"
  43. *ngFor="let book of myBooks">
  44. <div class="card-header" (click)="onAdd()"> {{ book.title }} </div>
  45. <div class="card-body">
  46. <!-- <h5 class="card-title">Dark card title</h5> -->
  47. <p class="card-text"> {{ book.content }} </p>
  48. </div>
  49. </div>
  50.  
  51. import { Component, OnInit, Input, EventEmitter } from '@angular/core';
  52. import { MyBookService } from '../mybook.service';
  53. import { Book } from '../book.model';
  54.  
  55. @Component({
  56. selector: 'app-home',
  57. templateUrl: './home.component.html',
  58. styleUrls: ['./home.component.css']
  59. })
  60. export class HomeComponent implements OnInit {
  61. myBooks: Book[];
  62.  
  63. constructor(private bookService: MyBookService) {}
  64.  
  65. ngOnInit() {
  66. this.myBooks = this.bookService.getBooks(); // Loading books
  67.  
  68. // Listening to changes
  69. this.bookService.booksChanged
  70. .subscribe(
  71. (book: Book[]) => {
  72. this.myBooks = book;
  73. }
  74. );
  75. }
  76.  
  77. onAdd() {
  78. this.bookService.addedBooks(this.myBooks);
  79. }
  80. }
  81.  
  82. import { Book } from './book.model';
  83. import { Subject } from 'rxjs';
  84. import { FavouriteService } from './favs.service';
  85. import { Injectable } from '@angular/core';
  86. import { Favourites } from './favs/fav.model';
  87.  
  88. @Injectable()
  89. export class MyBookService {
  90.  
  91. booksChanged = new Subject<Book[]>();
  92. bookSelected = new Subject<Book>();
  93.  
  94. constructor(private favService: FavouriteService) {}
  95.  
  96. private myBooks: Book[] = [
  97. new Book('A Monk who sold his ferrari', 'A burning sense of passion is the most potent fuel for your dreams.'),
  98. new Book('The Secret', 'You are already incredibly blessed, you just haven’t noticed.')
  99. ];
  100.  
  101. getBooks() {
  102. return this.myBooks.slice();
  103. }
  104.  
  105. addBooks(book: Book) {
  106. this.myBooks.push(book);
  107. this.booksChanged.next(this.myBooks.slice());
  108. }
  109.  
  110. addedBooks(favBook: Favourites[]) {
  111. this.favService.addedFavBooks(favBook);
  112. console.log('favBook: ', favBook);
  113. }
  114.  
  115. }
  116.  
  117. private favBooks: Favourites[] = [
  118. ];
  119.  
  120. getFavbooks() {
  121. return this.favBooks.slice();
  122. }
  123.  
  124. addedFavBooks(favBooks: Favourites[]) {
  125. this.favBooks.push(...favBooks);
  126. this.favBooksChanged.next(this.favBooks.slice());
  127. }
  128. }
  129.  
  130. <div class="container mt-3">
  131. <h3 class="mb-3">My Favourties: </h3>
  132. </div>
  133.  
  134. <!-- output -->
  135. <div class="card text-white bg-dark" style="max-width: 18rem;"
  136. *ngFor="let favBook of favBooks">
  137. <div class="card-header"> {{ favBook.title }} </div>
  138. <div class="card-body">
  139. <p class="card-text"> {{ favBook.content }} </p>
  140. </div>
  141. </div>
  142.  
  143. import { Component, OnInit } from '@angular/core';
  144. import { FavouriteService } from '../favs.service';
  145. import { Favourites } from './fav.model';
  146.  
  147. @Component({
  148. selector: 'app-favs',
  149. templateUrl: './favs.component.html',
  150. styleUrls: ['./favs.component.css']
  151. })
  152. export class FavsComponent implements OnInit {
  153. favBooks: Favourites[];
  154. favBook: Favourites;
  155.  
  156. constructor(private favService: FavouriteService) {}
  157.  
  158. ngOnInit() {
  159. // this.myBooks = this.bookService.addedBooks(); // Loading books
  160. this.favBooks = this.favService.getFavbooks();
  161.  
  162. this.favService.favBooksChanged
  163. .subscribe(
  164. (favs: Favourites[]) => {
  165. this.favBooks = favs;
  166. }
  167. );
  168. }
  169. }
  170.  
  171. <!-- loading form -->
  172. <app-form-input></app-form-input>
  173.  
  174. <!-- output -->
  175. <div class="card text-white bg-dark" style="max-width: 18rem;"
  176. *ngFor="let book of myBooks">
  177. <div class="card-header" (click)="onAdd(book)"> {{ book.title }} </div>
  178. <div class="card-body">
  179. <!-- <h5 class="card-title">Dark card title</h5> -->
  180. <p class="card-text"> {{ book.content }} </p>
  181. </div>
  182. </div>
  183.  
  184. import { Component, OnInit, Input, EventEmitter } from '@angular/core';
  185. import { MyBookService } from '../mybook.service';
  186. import { Book } from '../book.model';
  187.  
  188. @Component({
  189. selector: 'app-home',
  190. templateUrl: './home.component.html',
  191. styleUrls: ['./home.component.css']
  192. })
  193. export class HomeComponent implements OnInit {
  194. myBooks: Book[];
  195.  
  196. constructor(private bookService: MyBookService) {}
  197.  
  198. ngOnInit() {
  199. this.myBooks = this.bookService.getBooks(); // Loading books
  200.  
  201. // Listening to changes
  202. this.bookService.booksChanged
  203. .subscribe(
  204. (book: Book[]) => {
  205. this.myBooks = book;
  206. }
  207. );
  208. }
  209.  
  210. onAdd(selectedBook) {
  211. this.bookService.addedBooks([selectedBook]);
  212. }
  213. }
Add Comment
Please, Sign In to add comment