|
16 | 16 |
|
17 | 17 | <!-- Theme used for syntax highlighting of code --> |
18 | 18 | <link rel="stylesheet" href="../common/css/github-gist.css"> |
19 | | - |
| 19 | + <style> |
| 20 | + .container{ |
| 21 | + display: flex; |
| 22 | + } |
| 23 | + .col{ |
| 24 | + flex: 1; |
| 25 | + margin: 5px 20px; |
| 26 | + } |
| 27 | + </style> |
20 | 28 | </head> |
21 | 29 | <body> |
22 | 30 | <section class="angular-link"> |
@@ -58,130 +66,133 @@ <h2>Interpolation</h2> |
58 | 66 | <img class="reset" src="img/property-binding.png"> |
59 | 67 | <h2>Property Binding</h2> |
60 | 68 | </div> |
61 | | - <pre class="width-100-percent"><code class="hljs html" data-trim data-noescape> |
62 | | -<ul> |
63 | | - <li (click)="selectBook(firstBook)">{{ firstBook.title }}</li> |
64 | | - <li (click)="selectBook(secondBook)">{{ secondBook.title }}</li> |
65 | | -</ul> |
66 | | -<book-entry <mark>[book]="selectedBook"</mark>></book-entry > |
67 | | - </code></pre> |
68 | | - <pre class="width-100-percent"><code class="hljs typescript" data-trim data-noescape> |
69 | | -@Component(...) |
70 | | -export class BookOverviewComponent { |
71 | | - firstBook = new Book(...); |
72 | | - secondBook = new Book(...); |
73 | | - <mark>selectedBook</mark>: Book|null = null; |
| 69 | + <div style="margin-left:-200px;" class="container"> |
| 70 | + <div class="col"> |
| 71 | + <pre class="width-100-percent"><code class="hljs html" data-trim data-noescape> |
| 72 | + <!-- BookOverviewComponent (Parent) --> |
| 73 | + <ul> |
| 74 | + <li (click)="selectBook(firstBook)">{{ firstBook.title }}</li> |
| 75 | + <li (click)="selectBook(secondBook)">{{ secondBook.title }}</li> |
| 76 | + </ul> |
| 77 | + <book-entry <mark>[book]="selectedBook"</mark>></book-entry> |
| 78 | + </code></pre> |
| 79 | + <pre class="width-100-percent"><code class="hljs typescript" data-trim data-noescape> |
| 80 | + @Component(...) |
| 81 | + export class BookOverviewComponent { |
| 82 | + firstBook = new Book(...); |
| 83 | + secondBook = new Book(...); |
| 84 | + <mark>selectedBook</mark>: Book|null = null; |
74 | 85 |
|
75 | | - selectBook(book:Book):void { |
76 | | - this.selectedBook = book; |
77 | | - } |
78 | | -} |
79 | | - </code></pre> |
| 86 | + selectBook(book:Book):void { |
| 87 | + this.selectedBook = book; |
| 88 | + } |
| 89 | + } |
| 90 | + </code></pre> |
| 91 | + </div> |
| 92 | + <div class="col"> |
| 93 | + <pre class="width-100-percent"><code class="hljs html" data-trim data-noescape > |
| 94 | + <!-- BookDetailsComponent (Child) --> |
| 95 | + <div>Title: {{ <mark>book</mark>?.title }}</div> |
| 96 | + <div>Author: {{ <mark>book</mark>?.author }}</div> |
| 97 | + </code></pre> |
| 98 | + <pre class="width-100-percent"><code class="hljs typescript" data-trim data-noescape> |
| 99 | + @Component(...) |
| 100 | + export class BookEntryComponent { |
| 101 | + <mark>@Input() book: Book|null;</mark> |
| 102 | + } |
| 103 | + </code></pre> |
| 104 | + |
| 105 | + </div> |
| 106 | + </div> |
80 | 107 |
|
81 | 108 | <a href="https://angular.io/guide/property-binding" class="references" target="_blank">Angular Docs</a> |
82 | 109 | <aside class="notes"> |
| 110 | + To define a property we add the Input() decorator to the field of a class<br> |
| 111 | + The name of the property is defined through the name of the field<br> |
| 112 | + The operator in book?.author is for optional property access. So we don't get an error if book is null<br><br> |
| 113 | + |
83 | 114 | With property bindings data is passed from parent to child component<br> |
84 | 115 | Property bindings are always surrounded by [] brackets<br> |
85 | 116 | The left side of the = is the name of the property as defined by the child.<br> |
86 | 117 | The right side of the = is the name of the parent component's field to pass to the child.<br> |
87 | 118 | </aside> |
88 | 119 | </section> |
89 | | - <section> |
| 120 | + <section> |
90 | 121 | <div class="title-with-image"> |
91 | 122 | <img class="reset" src="img/property-binding.png"> |
92 | 123 | <h2>Property Binding</h2> |
93 | | - <img class="small-img" src="img/property.svg"> |
94 | 124 | </div> |
95 | | - <pre class="width-100-percent"><code class="hljs html" data-trim data-noescape> |
96 | | -<ul> |
97 | | - <li (click)="selectBook(firstBook)">{{ firstBook.title }}</li> |
98 | | - <li (click)="selectBook(secondBook)">{{ secondBook.title }}</li> |
99 | | -</ul> |
100 | | -<book-entry <mark>[book]</mark>="selectedBook"></book-entry > |
101 | | - </code></pre> |
102 | | - |
103 | | - <pre class="width-100-percent"><code class="hljs typescript" data-trim data-noescape> |
104 | | -@Component(...) |
105 | | -export class BookEntryComponent { |
106 | | - <mark>@Input() book: Book|null;</mark> |
107 | | -} |
108 | | - </code></pre> |
109 | | - <pre class="width-100-percent"><code class="hljs html" data-trim data-noescape> |
110 | | - <div>Title: {{ <mark>book</mark>?.title }}</div> |
111 | | - <div>Author: {{ <mark>book</mark>?.author }}</div> |
112 | | - </code></pre> |
113 | | - <a href="https://angular.io/guide/property-binding" class="references" target="_blank">Angular Docs</a> |
114 | | - <aside class="notes"> |
115 | | - To define a property we add the Input() decorator to the field of a class<br> |
116 | | - The name of the property is defined through the name of the field<br> |
117 | | - The operator in book?.author is for optional property access. So we don't get an error if book is null |
118 | | - </aside> |
| 125 | + <img src="img/property.svg"> |
| 126 | + <p><a href="https://v17.angular.io/guide/inputs-outputs" class="references" target="_blank">Angular Docs</a></p> |
119 | 127 | </section> |
120 | 128 | <section> |
121 | 129 | <div class="title-with-image"> |
122 | 130 | <img class="reset" src="img/event-binding.png"> |
123 | 131 | <h2>Event Binding</h2> |
124 | 132 | </div> |
125 | | - <pre class="width-100-percent"><code class="hljs html" data-trim data-noescape> |
126 | | -<book-entry [currentBook]="books[0]" |
127 | | - (selectBook)=<mark>"rentBook($event)"</mark>> |
128 | | -</book-entry> |
129 | | -<book-entry [currentBook]="books[1]" |
130 | | - (selectBook)=<mark>"rentBook($event)"</mark>> |
131 | | -</book-entry> |
132 | | - </code></pre> |
133 | | - <pre class="width-100-percent"><code class="hljs typescript" data-trim data-noescape> |
134 | | -@Component(...) |
135 | | -export class BookOverviewComponent { |
136 | | - books = [new Book(...), new Book(...)]; |
137 | | - rentedBook: Book|null = null; |
| 133 | + <div style="margin-left:-200px;" class="container"> |
| 134 | + <div class="col"> |
| 135 | + <pre class="width-100-percent"><code class="hljs html" data-trim data-noescape> |
| 136 | + <!-- BookOverviewComponent (Parent) --> |
| 137 | + <book-entry [currentBook]="books[0]" |
| 138 | + (selectBook)=<mark>"rentBook($event)"</mark>> |
| 139 | + </book-entry> |
| 140 | + <book-entry [currentBook]="books[1]" |
| 141 | + (selectBook)=<mark>"rentBook($event)"</mark>> |
| 142 | + </book-entry> |
| 143 | + </code></pre> |
| 144 | + <pre class="width-100-percent"><code class="hljs typescript" data-trim data-noescape> |
| 145 | + @Component(...) |
| 146 | + export class BookOverviewComponent { |
| 147 | + books = [new Book(...), new Book(...)]; |
| 148 | + rentedBook: Book|null = null; |
138 | 149 |
|
139 | | - <mark>rentBook(book: Book)</mark> { |
140 | | - this.rentedBook = book; |
141 | | - } |
142 | | -} |
143 | | - </code></pre> |
| 150 | + <mark>rentBook(book: Book)</mark> { |
| 151 | + this.rentedBook = book; |
| 152 | + } |
| 153 | + } |
| 154 | + </code></pre> |
| 155 | + </div> |
| 156 | + <div class="col"> |
| 157 | + <pre class="width-100-percent"><code class="hljs html" data-trim data-noescape> |
| 158 | + <!-- BookDetailsComponent (Child) --> |
| 159 | + <div>Book title: {{ currentBook.title }}</div> |
| 160 | + <button (click)="onSelectBtnClick()">Rent the book</button> |
| 161 | + </code></pre> |
| 162 | + <pre class="width-100-percent"><code class="max-height-unset typescript" data-trim data-noescape> |
| 163 | + @Component(...) |
| 164 | + export class BookEntryComponent { |
| 165 | + @Input() currentBook: Book; |
| 166 | + <mark>@Output() selectBook = new EventEmitter<Book>();</mark> |
| 167 | + |
| 168 | + onSelectBtnClick(): void { |
| 169 | + this.selectBook.emit(this.currentBook); |
| 170 | + } |
| 171 | + } |
| 172 | + </code></pre> |
| 173 | + </div> |
| 174 | + </div> |
144 | 175 | <a href="https://angular.io/guide/event-binding" class="references" target="_blank">Angular Docs</a> |
145 | 176 | <aside class="notes"> |
146 | 177 | With event bindings data is passed from child to parent component<br> |
147 | 178 | Event bindings are always surrounded by () brackets<br> |
148 | 179 | The left side of the = is the name of the event as defined by the child.<br> |
149 | 180 | The right side of the = is the name of the parent component's method to handle the event.<br> |
150 | | - $event is a keyword to pass the events payload as method param |
| 181 | + $event is a keyword to pass the events payload as method param<br><br> |
| 182 | + |
| 183 | + To define an event we add the Output() decorator to a field of a class<br> |
| 184 | + The name of the event is defined through the name of the field<br> |
| 185 | + We assign a EventEmitter to the field. On this EventEmitter we can call emit() to trigger the event. |
| 186 | + EventEmitter can be parametrized to determine the type of the event's payload. |
151 | 187 | </aside> |
152 | 188 | </section> |
153 | 189 | <section> |
154 | 190 | <div class="title-with-image"> |
155 | 191 | <img class="reset" src="img/event-binding.png"> |
156 | 192 | <h2>Event Binding</h2> |
157 | | - <img class="small-img" src="img/event.svg"> |
158 | 193 | </div> |
159 | | - <pre class="width-100-percent"><code class="hljs html" data-trim data-noescape> |
160 | | -<book-entry [currentBook]="books[0]" |
161 | | - <mark>(selectBook)="rentBook($event)"</mark>></book-entry> |
162 | | - </code></pre> |
163 | | - <pre class="width-100-percent"><code class="max-height-unset typescript" data-trim data-noescape> |
164 | | -@Component(...) |
165 | | -export class BookEntryComponent { |
166 | | - @Input() currentBook: Book; |
167 | | - <mark>@Output() selectBook = new EventEmitter<Book>();</mark> |
168 | | - |
169 | | - onSelectBtnClick(): void { |
170 | | - this.selectBook.emit(this.currentBook); |
171 | | - } |
172 | | -} |
173 | | - </code></pre> |
174 | | - <pre class="width-100-percent"><code class="hljs html" data-trim data-noescape> |
175 | | - <div>Book title: {{ currentBook.title }}</div> |
176 | | - <button (click)="onSelectBtnClick()">Rent the book</button> |
177 | | - </code></pre> |
178 | | - <a href="https://angular.io/guide/event-binding" class="references" target="_blank">Angular Docs</a> |
179 | | - <aside class="notes"> |
180 | | - To define an event we add the Output() decorator to a field of a class<br> |
181 | | - The name of the event is defined through the name of the field<br> |
182 | | - We assign a EventEmitter to the field. On this EventEmitter we can call emit() to trigger the event. |
183 | | - EventEmitter can be parametrized to determine the type of the event's payload. |
184 | | - </aside> |
| 194 | + <img src="img/event.svg"> |
| 195 | + <p><a href="https://v17.angular.io/guide/inputs-outputs" class="references" target="_blank">Angular Docs</a></p> |
185 | 196 | </section> |
186 | 197 | <section> |
187 | 198 | <h4>Input (Data) and Output (Event) Binding</h4> |
|
0 commit comments