Skip to content

Commit 11fa64e

Browse files
BaggersIOrobinboehm
authored andcommitted
Use the async pipes
1 parent 61eee31 commit 11fa64e

File tree

6 files changed

+49
-5
lines changed

6 files changed

+49
-5
lines changed

src/app/app.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<h1>
2-
{{title}}
1+
<h1 tooltip="Super App!">
2+
{{title | shout:'!!!111📢💥'}}
33
</h1>
44

55
<router-outlet></router-outlet>

src/app/app.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import { HttpClientModule } from '@angular/common/http';
55

66
import { AppComponent } from './app.component';
77
import { AppRoutingModule } from './app-routing.module';
8+
import { ShoutPipe } from './shared/shout.pipe';
9+
import { TooltipDirective } from './shared/tooltip.directive';
810

911
@NgModule({
1012
declarations: [
1113
AppComponent,
14+
ShoutPipe,
15+
TooltipDirective,
1216
],
1317
imports: [
1418
BrowserModule,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<ul>
2-
<li *ngFor="let book of books">
2+
<li *ngFor="let book of books | async">
33
<a [routerLink]="[book.isbn]">{{book.title}}</a>
44
</li>
55
</ul>
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22
import { Book } from '../shared/book';
33
import { BookDataService } from '../shared/book-data.service';
4+
import { Observable } from 'rxjs';
45

56
@Component({
67
selector: 'book-list',
@@ -9,15 +10,16 @@ import { BookDataService } from '../shared/book-data.service';
910
})
1011
export class BookListComponent implements OnInit {
1112

12-
books: Book[] = [];
13+
books: Observable<Book[]>;
14+
1315

1416
constructor(private bookService: BookDataService) {
1517

1618
}
1719

1820
ngOnInit() {
1921

20-
this.bookService.getBooks().subscribe(books => this.books = books);
22+
this.books = this.bookService.getBooks();
2123
}
2224

2325
}

src/app/shared/shout.pipe.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
3+
@Pipe({
4+
name: 'shout'
5+
})
6+
export class ShoutPipe implements PipeTransform {
7+
8+
transform(value: any, additional: string = '!!!'): any {
9+
10+
return `${value.toString().toUpperCase()}${additional}`;
11+
}
12+
13+
}

src/app/shared/tooltip.directive.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Directive, Input, HostListener } from '@angular/core';
2+
3+
@Directive({
4+
selector: '[tooltip]'
5+
})
6+
export class TooltipDirective {
7+
8+
@Input()
9+
tooltip: string;
10+
11+
constructor() { }
12+
13+
@HostListener('mouseenter')
14+
onMouseEnter() {
15+
16+
console.log(this.tooltip);
17+
}
18+
19+
@HostListener('mouseleave')
20+
onMouseLeave() {
21+
22+
console.log(this.tooltip);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)