The rebel attack has begun! At the moment, all our turrets are still operational, but some of them have taken damage. We need to know which ones are in need of repair. Luckily for us, this information is available to us in our data banks...we just need to bring it to our dashboard to quickly deploy repair droids. To the data banks!
-
Open up our data banks. Go into
turret-api
, runseed.js
andserver.js
...but don't forget to install those npm modules! -
Go to
localhost:3000/api/turret/4
to see how much damage our 4th turret has. We have 16, so see how much damage the other ones have. -
Go into
imperial-starfleet
and start up our front end. -
Import HTTP, as we did in the previous lesson, into the InfoWindowComponent. Also import the
rxjs
toPromise
feature. -
Put
http
into the constructor of InfoWindowComponent, as we did in the previous lesson. -
Create a
findTurret()
function that passes in aturretNumber
that looks very 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 into a variable called dataBanks
(not response
like the findCharacter()
function). Make sure you declare dataBanks
at the top of your InfoWindowComponent
class, first.
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 this function inside the
ngOnInit
function, with a hardcoded value of4
for now. -
Change the
info-window.html
template to be an unordered list with two list items: one with a double bracket reference todataBanks.turretNumber
, and one with a reference todataBanks.damage
. -
Test your work 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's routerLink. -
Add a
/:id
to the path forinfo
inapp-routing.module.ts
. -
Now, we need to handle this new
turretNumber
in the InfoWindowComponent. ImportActivatedRoute
and include it in yourconstructor
as we did in the URL Params lesson. -
Add a
this.route.params.forEach
function similar to the URL Params lesson's, and tuck thethis.findTurret()
function inside it. 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. 2 damage? Not too bad! Click on the bottom-right turret. 9 damage? Let's get that repair droid on its way! Victory will be ours!
- 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. Good ol' async JS, right? You can fix this with a well-placed*ngIf
*.
*Look for "Built-in Directives"