Outline
Clone Repository
In this bonus exercise we are going to be using a Git repository that is public on GitHub. As such, you'll need to clone the repository to your local machine using Git.
Some prerequisites:
Clone the repository using the Git command:
git clone https://github.com/blove/thinkster-directives.git
The default branch is the master
branch. This branch of the repository contains the solution. So, DON'T LOOK at the code yet.
Next, checkout the exercises/03-12-mos-eisley-cantina
branch using the following Git command:
git checkout exercises/03-12-mos-eisley-cantina
Next, install the dependencies using either npm
or yarn
:
npm install
Finally, start the application using the start
script:
npm start
Tasks
- Open the src/app/shared/directives/greedo.directive.ts file.
- Using the
@Input()
decorator, create a newfire
property that is typedboolean
. Specify the binding name to the@Input()
function to be custom rather than using the "fire" property name. Let's use the directive's attribute: "swrGreedo". - In the
ngOnChanges()
lifecycle method get thenativeElement
using the injectedElementRef
class instance. - Also in the
ngOnChanges()
lifecycle method set thesrc
of the<img>
element to the value of thesrc
locally-scoped variable (this is defined for you). For some added challenge, only set thesrc
after 500 milliseconds. - Open the src/app/shared/directives/han.directive.ts file.
- Just like we did above in step 2, create a new
fire
property that is decorated using the@Input()
decorator, specifying a custom binding name to match the directive attribute selector: "swrHan". - Again, In the
ngOnChanges()
lifecycle method get thenativeElement
using the injectedElementRef
class instance. - Also in the
ngOnChanges()
lifecycle method set thesrc
of the<img>
element to the value of thesrc
locally-scoped variable (this is defined for you). - Open the src/app/features/mos-eisley/containers/cantina/cantina.component.html file.
- In the template you will notice that the
<img>
elements have the appropriate attribute directives applied. However, we need to bind the attribute to thefire
property value that is declared in theCantinaComponent
class.
To test your solution you will need to go to the Cantina on Mos Eisley. You can access it via the application menu, or browse directly to it: http://localhost:4200/mos-eisley/cantina
Solution
If you want to see the solution to this exercise you can browse to the repository. Or, simply checkout the master
branch via:
git checkout master
Challenge
While you can use the window.setTimeout()
method in the GreedoDirective.ngOnChanges()
method to delay setting the src
property on the <img>
element, I suggest you try using RxJS's interval()
method to set an interval, and then complete the observable after the first next notification.