You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this chapter, we are age going to use the `@Input()` annotation.
239
+
:marked
240
+
.l-main.section
209
241
:marked
210
242
## #3: Announcing a job — parent to child communication with multiple data binding
211
243
212
-
_Content_
244
+
The aim of the Hero Job Board application is to use our heroes superpower. So, let's create job requests for them! In this scenario, we enable the person in need to
245
+
create a job request and send them to heroes by clicking the **Ask** button.
213
246
214
-
## #4: A hero takes the job — child holds a direct reference to parent — antipattern
247
+
We are going to use data binding from `HeroJobBoard` (parent) to `InvitedHero` (child), similarly as we did in the previous scenario. However, this time we bind another
248
+
property, and demonstrate that in the child template we can use the input property indirectly, and Angular still detects the changes, and updates the UI accordingly.
215
249
216
-
_Content_
250
+
We add the `request` property to `InvitedHero` so that `HeroJobBoard` could pass it a new job request. The `getRequest()` method leverages this property to fall back
251
+
to a default lable when no job request is available:
`HeroJobBoard` defines an input box and and a button within its template to send the new job request to heroes. We modify this part of the template so that a job request
262
+
should contain at least one non-whitespace character to send:
263
+
264
+
+makeExample('component-communication/ts/send-job-request/fragments/input.html', null, 'hero-job-board.ts (extract from template)')
265
+
266
+
:marked
267
+
The value of the input control is refreshed when the `jobRequest` properties value is changed — it is done within the code of `HeroJobBoard` — or users type
268
+
into the text field. To store the value of the text field in the `box` local variable, we're using the technique demonstrated in the [User Input](./user-input.html) chapter.
269
+
The **Ask** button uses this `box` variable to define an expression that disables the button unless at least one non-whitespace charecter is typed in.
270
+
271
+
.alert.is-helpful
272
+
:marked
273
+
The expression uses the Angular "Elvis" operator (`?.`). This is a fluent and convenient way to guard against null and undefined values in property paths.
274
+
It protects against a view render failure if `box` is null.
275
+
:marked
276
+
When users click the **Ask** button, the `announceJob()` method is invoked with the content of the input text field.
When announcing a new job request, the `jobRequest` property is set. The value of this property is deleted (set to null) within `inviteAllHeroes()`,
282
+
so users can click the **Invite heroes** to reset the application, as this action will delete the job request, too.
283
+
284
+
To push the `jobRequest` value to all `InvitedHero` instances, we need to modify the `HeroJobBoard` template:
285
+
+makeExample('component-communication/ts/send-job-request/fragments/request.html', null, 'hero-job-board.ts (extract from template)', {blk:/(request)/g})
286
+
287
+
:marked
288
+
We're almost done. Now, data binding works as expected, but to make the app reflect its current workflow state, we add a few cosmetics. In `HeroJobBoard` we define
289
+
the `getJobStatus()` function that retrieves an appropriate label to show instead of "Responding reroes":
0 commit comments