After this training, developers will be able to:
- set up an angular app.
- use
$http
to access a RESTful API. - practice routing in Angular using
ngRoute
.
Deliberate practice is a really effective way to build skills. You've learned about a lot of key pieces of an Angular app, and now it's time to put those pieces together, again. You'll be expected to be able to build an Angular app from scratch.
In this lab, you'll be creating a simple library app to keep track of books.
When a user goes to /
, they should see a list of all of the books in the API. When a user goes to /books/:id
, they should see a single book. On the /books/:id
page, a user should be able to edit or delete a book.
Your data (a list of books) is available at https://super-crud.herokuapp.com/books
. You and your classmates will all be working with the shared API; keep that in mind as you make changes. If there are no books left or far too many books, feel free to reset the database by clicking the reset button. Don't do this without warning your classmates over Slack, though. They might be puzzled why their newly created book resources aren't appearing in the database.
Your finished product will:
- Route the user to an index page at
/
. That page will: * display all of the books. * show the image, title, author, and release date of each book. * include a link to the show book page on the title of each book. - Route the use to a show book page at
/books/:id
for any existing book id. The show page will: * display all of the data about the specific book. * have a delete button that deletes the specific book from the database and, when successfully deleted, redirects the user to the home page. * have an edit button that reveals a form for the user to edit the attributes of the book. * The form will have a save button that sends the edits to the database and, when successfully updated, redirects the user to the home page. * have a cancel button that does not save any of the changes the user just made.
There are three branches in this repo that you might want to pay attention to: master
and solutions
.
The master
branch has these instructions, with no starter code. As you start building, you'll want to reference your previous Angular projects to make sure you're setting up your app correctly.
The solutions
branch has a fully built-out application that meets all the expectations above. Reference it if you're really stuck, but remember you may have your app set up differently!
You can work directly from the master
branch or create another branch for your work!
- Fork and clone this repo.
- Change directories into
angular-books-crud-lab
. - Start creating the files you need for your project! (
index.html
andapp.js
are safe bets to start.) - Install
budo
, which you'll use to run a simple server for this project:npm install -g budo
- Run
budo -P --host=localhost --open
from the Terminal (inside your project directory). This starts a simple server that by default will serve up yourindex.html
on/
and any route it doesn't recognize (like/books
). Note: Yourindex.html
must be in your main project directory for this server setup.
- Commit frequently!
- Once you add client-side routing, Angular will need to be served from a server. Make sure you're running
budo
, and that the URL you're looking at starts withlocalhost
, NOTfile://
. - The books api is set up with RESTful routes, so follow RESTful routing conventions. You can test endpoints with Postman or cURL, or by setting up a simple request to trigger them.
- Look at (
console.log
) the data you get back from each$http
request!
This is what your directory structure should look like. (It's okay if it's not exactly like this, but this is a structure that makes a lot of sense.)
.
├── README.md
├── index.html // IMPORTANT
├── public
│ ├── images
│ │ └── logo.jpg
│ ├── scripts
│ │ ├── app.js
│ │ └── controllers
│ │ ├── booksIndexController.js
│ │ └── booksShowController.js
│ └── styles
│ └── styles.css
└── templates
├── books-index.html
└── books-show.html
Here's a hint on how to change the page url from within a controller. (You don't necessarily need to do this, depending on your solution.)
Inject the $location
service, and use its path
method.
// inside GoatsShowController, we want to send the user back to goats index (home page) automatically
$location.path('/');
Here is a hint on the format of template URLs when using local HTML files.
Give the full file name: /templates/goats-show.html
.
- Push this to Amazon AWS and monetize traffic to make millions of dollars.
- Allow the user to edit the book image: allow the user to change the URL for the book image.
- Add filters to organize the books index page: add a search bar to filter the books by your search, or buttons to sort them alphabetically by author name or book title.