<span>{{ currentBook.author }}</span>
@Component(...)
export class BookEntryComponent {
currentBook = { author:'John Example', title: 'Angular in a nutshell' };
}
Angular Docs
<ul>
<li (click)="selectBook(firstBook)">{{ firstBook.title }}</li>
<li (click)="selectBook(secondBook)">{{ secondBook.title }}</li>
</ul>
<book-entry [book]="selectedBook"></book-entry >
@Component(...)
export class BookOverviewComponent {
firstBook = new Book(...);
secondBook = new Book(...);
selectedBook: Book|null = null;
selectBook(book:Book):void {
this.selectedBook = book;
}
}
Angular Docs
<ul>
<li (click)="selectBook(firstBook)">{{ firstBook.title }}</li>
<li (click)="selectBook(secondBook)">{{ secondBook.title }}</li>
</ul>
<book-entry [book]="selectedBook"></book-entry >
@Component(...)
export class BookEntryComponent {
@Input() book: Book|null;
}
<div>Title: {{ book?.title }}</div>
<div>Author: {{ book?.author }}</div>
Angular Docs
<book-entry [currentBook]="books[0]"
(selectBook)="rentBook($event)">
</book-entry>
<book-entry [currentBook]="books[1]"
(selectBook)="rentBook($event)">
</book-entry>
@Component(...)
export class BookOverviewComponent {
books = [new Book(...), new Book(...)];
rentedBook: Book|null = null;
rentBook(book: Book) {
this.rentedBook = book;
}
}
Angular Docs
<book-entry [currentBook]="books[0]"
(selectBook)="rentBook($event)"></book-entry>
@Component(...)
export class BookEntryComponent {
@Input() currentBook: Book;
@Output() selectBook = new EventEmitter<Book>();
onSelectBtnClick(): void {
this.selectBook.emit(this.currentBook);
}
}
<div>Book title: {{ currentBook.title }}</div>
<button (click)="onSelectBtnClick()">Rent the book</button>
Angular Docs
<input type="text" [(ngModel)]="currentBook.author">
@Component(...)
export class BookEntryComponent {
currentBook = { author:'John Example', title: 'Angular in a nutshell' };
}
Angular Docs
<div *ngIf="book">
<p>{{ book.description }}</p>
</div>
@if(book)
{
<p>{{ book.description }}</p>
}
Removes element from the DOM if the book is not present.
<ul>
<li *ngFor="let myItem of myList">{{ myItem.someProperty }}</li>
</ul>
<ul>
@for (myItem of myList; track $index)
{
<li> {{ myItem.someProperty }} </li>
}
</ul>
Creates an element for every item of the collection.
@Component({
selector: 'app-book-overview',
template: `<app-book-entry
*ngFor="let book of books"
[currentBook]="book"
(selectBook)="rentBook($event)">
</app-book-entry>`
})
export class BookOverviewComponent {
books = [new Book(...), new Book(...)];
rentedBook: Book|null = null;
rentBook(book: Book) {
this.rentedBook = book;
}
}
@Component({
selector: 'app-book-entry',
template: `<div *ngIf="currentBook">
<div>Book author: {{ currentBook.author }}</div>
<div>Book title: {{ currentBook.title }}</div>
<button (click)="onSelectBtnClick()">Rent book</button>
</div>`
})
export class BookEntryComponent {
@Input() currentBook: Book;
@Output() selectBook = new EventEmitter<Book>();
onSelectBtnClick(): void {
this.selectBook.emit(this.currentBook);
}
}