The rebel attack has begun. At the moment, all of our turrets are still operational, but some may have been damaged. We need to know which ones need to be repaired. Luckily, this information is available in our data banks; we just need to bring it to our dashboard to quickly deploy repair droids.
-
Open up our data banks. Navigate into
turret-api
and runseed.js
andserver.js
— don't forget to install thenpm
modules. -
Go to
localhost:3000/api/turret/4
to see how much damage our fourth turret has incurred. We have 16, so see how much damage the other ones have as well. -
Navigate into
imperial-starfleet
and start up our front end. -
Import HTTP into the
InfoWindowComponent
, like we did in the previous lesson. Additionally, import therxjs
toPromise
feature. -
Put
http
into the constructor ofInfoWindowComponent
. -
Create a
findTurret()
function that passes in aturretNumber
that looks similar to ourfindCharacter()
function from the previous lesson. Make yourget
request to the same URL we used in Step 2.
6a) Save your JSON results from the findTurret()
function in a variable called dataBanks
(Note: Not results
like the findCharacter()
function). Make sure you first declare dataBanks
at the top of your InfoWindowComponent
class.
Note: You want to
console.log()
theresponse.json()
value before you save it todataBanks
. It does NOT have the same format as the Star Wars API response.
-
Call the function inside the
ngOnInit
function with a hard-coded value of4
, for now. -
Change the
info-window.html
template to an unordered list with two list items: one with a double-bracket reference todataBanks.turretNumber
and one with a reference todataBanks.damage
. -
Test the work you've done so far in the browser.
-
In
death-square.component.html
, give each turret aturretNumber
like so:[turretNumber]="1"
. -
Import the
Input
module from Angular core inturret.component.ts
. -
Take this value into the
turret.component.ts
TurretComponent
class like so:@Input('turretNumber') turretNumber: number;
. -
Add the
turretNumber
variable in double curly braces to yourturret.component.html
template'sRouterLink
. -
Add a
/:id
to theinfo
path inapp-routing.module.ts
. -
Now, we need to handle this new
turretNumber
in theInfoWindowComponent
. ImportActivatedRoute
and include it in yourconstructor
, like we did in the URL Params lesson. -
Add a
this.route.params.forEach
function and tuck thethis.findTurret()
function inside it, similar to the URL Params lesson. Don't forget to switch out the hard-coded4
for theparam.id
coming from your route params. -
Now, let's test it in the browser. Click on the top-left turret. Two damage? Not too bad. Click on the bottom-right turret. Nine damage? Let's get that repair droid on its way.
If you look in Dev Tools, you'll notice it complaining about dataBanks
not being defined until we get the result back from our API. You can fix this with a well-placed *ngIf
*.
*Look for "Built-In Directives."