In this unit you will be building a full MVC-style frontend web application to display the content of your API. You will be using AngularJS, a frontend framework maintained by Google, and still the most popular framework for building web applications today.
- Understand how views, controllers and models interact in Angular.
- Use factories to maintain data consistency across your application.
- Create a rich, dynamic messaging platform.
- Use your own API to reinforce the value of API design.
- Successfully make HTTP requests in Angular.
Developing modern front-end web applications requires us to manage complex patterns of data on the front-end. The data (for example, the number of favorites on a tweet, whether a button has been selected or not) constantly change as new information becomes available from the server or as users interact with the data. Frameworks give us the structure to more easily design applications that handle this complexity.
Angular is the currently the most popular front-end JavaScript framework using the Model-View-Controller pattern. Coding Horror - Understanding MVC
Angular uses two-way data binding to ensure that any data-related changes affecting the underlying data model immediately update the relevant view or views (the UI that the user interacts with). Any changes to the view (for example by a user) immediately updates the relevant data model.
'Controllers' AngularJS Controller Tutorial join the dots between the 'data' in the view (that the user interacts with) and data in the underlying 'model'
We must write the models, views and controllers (Angular docs - Controllers) ourselves as developers - Angular gives us plenty of tools to do so as well as a structure it expects us to follow.
- Angular documentation can be obscure, so ensure you balance interpreting the documentation with reading posts & tutorials by other developers - Use AngularJS to Power Your Web Application
- Forget about React's clear and helpful error messages - Angular's errors are a different beast. If you feel like your error message is unreadable, notice that each error message has a URL link at the top. Clicking the link takes you to a more detailed error page which is usually much more helpful than the message shown in console.
- Run
npm installto install server-side dependencies - Run
bower installto install browser-side dependencies - Run
npm startto start your server. Open your browser to the following address:
http://localhost:3000
- You can run the tests either by opening
test/test.htmlin the browser or running this command in the terminal.
npm test
- Additionally, be sure to run the app in your browser and use it to verify its functionality.
npm start
open http://localhost:3000/
- Modify the
HomeControllerscope have anameproperty. Set thenameproperty to your name. - Modify the
home.htmlpartial to display the value set to thenameproperty.
- Create another route at
/aboutthat displays theabout.htmlpartial. - Create another module named
Codesmith.AboutController(place inside./client/controllers/aboutController.jsand be sure to include this new file on the page via a<script>tag). - Create a controller named
AboutControllerinside theCodesmith.AboutControllermodule - Add this module to you app's dependencies in
myApp. - Modify the
about.htmlpartial to display the name property - Inside the
about.htmlpartial, add the directiveng-modelto the tag. Set the value of theng-modeldirective toname - Type into the input field. If done correctly, the input should be rendered alongside the input field
- Add links to navigate between these views using
ng-href. This is a directive in thengRoutemodule.
- Create a separate module named
Codesmith.UserFactory(place inside./client/factories/userFactory.js) - Create a factory named
UserFactoryinside theCodesmith.UserFactorymodule. This will return an object with anameandageproperty, which should be equal to your name and age. - Make sure this new Factory is included by a
<script>tag and added to the dependencies. Which module should be dependent on this factory? Is itmyApp? - Inject
UserFactoryinsideAboutController. - Add a
ng-submitdirective to the on theabout.htmlpartial - Add a function called
saveinAboutControllerthat, when submitted, saves thenameinside theUserFactoryobject. - Add an
agefield to the form. Which also gets saved. - Inject
UserFactoryinsideHomeController - Have the
HomeControllername property equal to theUserFactoryname such thatnamesaved in the/aboutpage is displayed. (You should be able to navigate between these without losing the changes to your UserFactory).
- Create another factory called
MessageFactorywhich we'll use to connect to the messaging API.
http://slack-server.elasticbeanstalk.com/messages
- Inside the
MessageFactory, create a method calledfetchthat makes a GET request to the API. The returned object should be the promise object returned from the$httprequest. - Inject this factory into the
HomeController. - When the user visits the home page, the
HomeControllershould get the messages and use the promise to store the array of messages inside themessagesproperty of theHomeControllerscope. - Use the
ng-repeatdirective to display all the messages in thehomepartial - Add a
postmethod to your factory which will make a post request to messages. This should also return the promise. - Add a text input to this page which will be used to post a new message to the API. When submitted, have the
HomeControllerget the new message from this input and call thepostmethod passing the new message as an argument. (Don't forget to send the name too). - Be sure to add your new message to the
messagesproperty and update the page with your message.
- Add a dropdown menu that enables the user to sort the data by different fields. This can be done with plain old JavaScript, but there is a cleaner way! Look up Angular filters.
- Add a search bar that can show only the messages that match the search phrase.
Many APIs have Angular plugins (a.k.a. modules) that let Angular apps use them very easily. Its ease of use should be on par with libraries like ng-route. All it takes is 3 simple steps:
- Include your JS file with a
<script>tag. - Add your module to their app's dependencies.
- Start using this module's directives and services.
Write a JS file with a new Angular module that contains an element directive (similar to a React component). Demonstrate its use by showing it in myApp. Create a new view for your API that shows styled data on this view. To be clear, all that should be required to get your API's data into myApp is these three steps:
- Include your JS file with a
<script>tag iniindex.html. - Add the new module to
myAppdependencies. - Placing the element directive (custom tag) into the view.
- Replace with ngRoute with ui-router.
- Support Markdown in your messaging platform. Have messages containing markdown render to HTML on your page. You'll need to compile it to HTML and then render it. Check out MarkdownJs and Angular's $sce service.
- Have the messages list update with new messages every 3 seconds.
- Use
ngAnimateto have a "fade" effect when you transition between pages.
