Exercise learning the foundations of Angular 2. This tutorial is based on Angular 2 alpha27 and the angular2-seed. In order to try it just clone the repo and follow the steps.
If you get stuck on any of the steps you can use the following branches, which contain the final solution for given chapter:
- Inside
init.tsimport the moduleapp, which is located in the same directory. You can point it using its relative location. - Name the
app/app.tscomponentapp, using the selector property of the component annotation. - Inside the
Viewannotation add propertytemplateUrl, which should point to theappcomponent's template. - Inside the
Appcomponent's template add the following markup:
<h1>Hello Angular2!</h1>Congratulations! Now you already have a working Angular 2 application! You can test it by using the terminal. Go to the angular2-github-app-bootstrap directory and type gulp serve.dev inside your terminal window.
The result should be as follows:
- Inside
app/app.tsimport thehomecomponent and add it as dependency of theAppcomponent.- The home component is located inside the directory
components/home. You can use its relative path. - All views accept directives as dependencies. You can define that the
Appcomponent requiresHomeas dependency by adding thedirectiveproperty of the object passed to the component annotation. The directive property accepts an array of directives as value.
- The home component is located inside the directory
- Inside
home.tsimport the following annotations and directives from the moduleangular2/angular2:Component,View,NgFor,NgIf. - Add
NgForandNgIfas dependencies of theHomecomponent, the same way you addedHomeas dependency for theAppcomponent. - Add
templateUrlproperty for theHomecomponent, which points to the appropriate location. - Add property called
selectorwith valuehometo the object passed to the component annotation of theHomecomponent. - Inside
app/app.htmladd theHomecomponent usinghomeelement (remove theHello Angular2!heading). - Define a property called
usersof theHomeclass, it should be of typestring[]and inside the constructor ofHomeit should be initialized with an empty array. - Open the template associated with the
Homecomponent and make the following changes:- Add property called
#currentuserto the input with iduser-input. This way you'll be able to reference the input usingcurrentuser. - Add a click handler to the button next to the input. The click event should call the method
addUserwith thecurrentusertext input - Add
*ng-fordirective to the firsttrelement inside the body of the table under theAll Userslegend. It should loop over allusersand display their name inside the link in the first cell (note: you can use the interpolate directive{{ }}). - To the link inside the first cell add a click handler. Once a click event occurs the
selectUsermethod should be invoked with thecurrentuseras argument. - To the button inside the second cell add a click handler, once a click event occurs invoke the
removeUsermethod with the current user as argument.
- Add property called
- Now open the
Homecomponent and implement the methodsaddUser,removeUseras follows:addUser(user)should add the new user to the list of users (i.e.usersarray). Optionally you can restrict theaddUseroperation to only not existing users. Do not forget to reset the value of thecurrentuserinput once you add it to the list of users.removeUser(user)should remove the given user from theusersarray.
Awesome! The second step is completed! If you haven't stopped gulp serve.dev just save the files you changed and refresh http://localhost:5555 in order to see the functionality you just implemented!
Did you notice that when you click on any of the users in the list on the right-hand side of the screen an error occurred? Now we're going to fix this!
- Inside
home.tsimport theUserDetailscomponent, which is located inside../../components/user-details/UserDetailsand add it as dependency of theHomecomponent. - Define a method called
selectUser(user:string), which implements the following logic:- Sets the
selectedUsertonull. - Sets the
loadingstatus to true. - Invokes the
getUsermethod with the currentuserpassed as argument.getUserwill return a promise, add resolve handler for the promise. The handler should accept a single argument - the user, which we got from the GitHub's API. Inside the callback set the value of theselectedUserand change theloadingstatus tofalse.
- Sets the
- Inside the template of the
Homecomponent uncomment the lines in the bottom and add*ng-ifdirective to the.spinnerelement with valueloading(i.e. the element should be visible when theloadingstatus istrue). - In to the
user-detailscomponent add two attributes:[user]attribute, which should point to the currentselectedUser*ng-ifattribute (directive), which should has as value the following boolean expression:selectedUser !== null.
- Open
UserDetails.tsand add the propertypropertiesto the object passed to theComponentannotation's call. It should be an array with a single value'user'. This way we state that we want to be able to pass properties to ourUserDetailscomponent using the[user]attribute. - Now open
user-details.htmland make the following additions:- Add property called
[src]to theimgelement. It should point to theuser.avatar_url. - To each second cell (i.e. the cells next to the labels "Username", "Followers", "Following", etc.) add the following attributes using the interpolation directive:
{{user.login}}{{user.followers}}{{user.following}}{{user.public_repos}}
- Add property called
- As last step, in order to provide slightly better user experience, inside the
removeUsermethod in theHomecomponent set the value of theselectedUsertonullif it is equal to the user we want to remove.
Thats it. You're done with this exercise! You can try it the same way you tried "Step 2"! Congratulations! Here is what you should see as final result:
This is the first iteration of the readme so there might be some typos, mistakes or tasks, which are not quite clear or too verbose. If you think you can improve the quality of the document just fork the repo and initiate a pull request, it will be greatly appreciated!
MIT


