Learn Angular: Build a Todo App
()
About this ebook
Angular is not just a framework, but rather a platform that empowers developers to build applications for the web, mobile, and the desktop.
This book contains a complete tutorial on building a todo app with Angular. Along the way, we'll learn about installation and setup, component architecture, adding a REST backend, routing, authentication, and much more.
This book is for all front-end developers who want to become proficient with Angular and its related tools. You'll need to be familiar with HTML and CSS and have a reasonable level of understanding of JavaScript in order to follow the discussion.
Jurgen van de Moere
Front-end Architect at The Force - specializing in JavaScript and AngularJS. Developer Expert at Google. Gymnast. Dad. Family man. Creator of Angular Express.
Related to Learn Angular
Related ebooks
Your First Week With Node.js Rating: 0 out of 5 stars0 ratingsApps and Services with .NET 8: Build practical projects with Blazor, .NET MAUI, gRPC, GraphQL, and other enterprise technologies Rating: 0 out of 5 stars0 ratingsBeginning Mobile Application Development in the Cloud Rating: 0 out of 5 stars0 ratingsUnleashing the Power of TypeScript Rating: 0 out of 5 stars0 ratingsJavaScript from Frontend to Backend: Learn full stack JavaScript development using the MEVN stack with quick and easy steps Rating: 0 out of 5 stars0 ratingsRapid Application Development With CakePHP Rating: 0 out of 5 stars0 ratingsPerformance Tools Rating: 0 out of 5 stars0 ratingsJavaScript: Best Practice Rating: 0 out of 5 stars0 ratingsThe Web Performance Collection Rating: 0 out of 5 stars0 ratingsLearning AngularJS Animations Rating: 4 out of 5 stars4/5Practical C Programming: Solutions for modern C developers to create efficient and well-structured programs Rating: 0 out of 5 stars0 ratingsAn Introduction to Website Performance: How to Outrun the Zombie Hordes: Undead Institute, #15 Rating: 0 out of 5 stars0 ratingsInstant Handlebars.js Rating: 0 out of 5 stars0 ratingsJump Start PHP Environment: Master the World's Most Popular Language Rating: 0 out of 5 stars0 ratingsLiving With Linux In the Industrial World Rating: 0 out of 5 stars0 ratingsResponsive Design High Performance Rating: 0 out of 5 stars0 ratingsPersistence in PHP with Doctrine ORM Rating: 0 out of 5 stars0 ratingsHTML, XHTML, and CSS: Your visual blueprint for designing effective Web pages Rating: 4 out of 5 stars4/5NW.js Essentials Rating: 0 out of 5 stars0 ratingsJump Start Sass: Get Up to Speed With Sass in a Weekend Rating: 0 out of 5 stars0 ratingsAjax Bible Rating: 3 out of 5 stars3/5Beginning DotNetNuke Skinning and Design Rating: 0 out of 5 stars0 ratingsWeb Scraping for SEO with Python Rating: 0 out of 5 stars0 ratingsPHP and MongoDB Web Development Beginner’s Guide Rating: 0 out of 5 stars0 ratingsjQuery Mobile Web Development Essentials - Third Edition Rating: 0 out of 5 stars0 ratingsDrupal Multimedia Rating: 4 out of 5 stars4/5Learn to Code With JavaScript Rating: 0 out of 5 stars0 ratingsNode Web Development, Second Edition Rating: 0 out of 5 stars0 ratingsNodeJS: Programmare Web-App Con Javascript: Programmazione Web, #3 Rating: 0 out of 5 stars0 ratingsLaravel 5.x Cookbook Rating: 0 out of 5 stars0 ratings
Programming For You
Narrative Design for Indies: Getting Started Rating: 4 out of 5 stars4/5Modern C++ Programming Cookbook Rating: 5 out of 5 stars5/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python for Finance Cookbook: Over 50 recipes for applying modern Python libraries to financial data analysis Rating: 0 out of 5 stars0 ratingsPython Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Python for Beginners: A Crash Course to Learn Python Programming in 1 Week Rating: 0 out of 5 stars0 ratings50 Recipes for Programming Angular Rating: 4 out of 5 stars4/5Learn JavaScript in 24 Hours Rating: 3 out of 5 stars3/5Django Building Dynamic Website With Django : A Complete Step By Step Guide To Learn to Build Modern Web Application with a Python Rating: 0 out of 5 stars0 ratingsLearn Algorithmic Trading: Build and deploy algorithmic trading systems and strategies using Python and advanced data analysis Rating: 0 out of 5 stars0 ratingsPython Projects for Kids Rating: 5 out of 5 stars5/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5Mastering C# and .NET Framework Rating: 5 out of 5 stars5/5Expert Delphi Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Deep Learning For Dummies Rating: 0 out of 5 stars0 ratingsHTML, CSS, & JavaScript All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsSQL All-in-One For Dummies Rating: 3 out of 5 stars3/5ITIL 4: Digital and IT strategy: Reference and study guide Rating: 5 out of 5 stars5/5
Reviews for Learn Angular
0 ratings0 reviews
Book preview
Learn Angular - Jurgen van de Moere
Preface
So, why Angular? Well, because it’s supported on various platforms (web, mobile, desktop native), it’s powerful, modern, has a nice ecosystem, and it’s just cool. Not convinced? Let's be a bit more eloquent, then:
Angular presents you not only the tools but also design patterns to build your project in a maintainable way. When an Angular application is crafted properly, you don’t end up with a tangle of classes and methods that are hard to modify and even harder to test. The code is structured conveniently and you won’t need to spend much time in order to understand what is going on.
It’s JavaScript, but better. Angular is built with TypeScript, which in turn relies on JS ES6. You don’t need to learn a totally new language, but you still receive features like static typing, interfaces, classes, namespaces, decorators etc.
No need to reinvent the bicycle. With Angular, you already have lots of tools to start crafting the application right away. You have directives to give HTML elements dynamic behavior. You can power up the forms using FormControl and introduce various validation rules. You may easily send asynchronous HTTP requests of various types. You can set up routing with little hassle. And there are many more goodies that Angular can offer us!
Components are decoupled. Angular strived to remove tight coupling between various components of the application. Injection happens in NodeJS-style and you may replace various components with ease.
All DOM manipulation happens where it should happen. With Angular, you don’t tightly couple presentation and the application’s logic making your markup much cleaner and simpler.
Testing is at the heart. Angular is meant to be thoroughly tested and it supports both unit and end-to-end testing with tools like Jasmine and Protractor.
Angular is mobile and desktop-ready, meaning you have one framework for multiple platforms.
Angular is actively maintained and has a large community and ecosystem. You can find lots of materials on this framework as well as many useful third-party tools.
So, we can say that Angular is not just a framework, but rather a platform that empowers developers to build applications for the web, mobile, and the desktop.
This book presents a complete project tutorial: building a todo app with Angular CLI. Along the way, you'll refactor the app to take advantage of some of Angular's best features.
Who Should Read This Book?
This book is for all front-end developers who wannt to get proficient with Angular and its related tools. You’ll need to be familiar with HTML and CSS and have a reasonable level of understanding of JavaScript in order to follow the discussion.
Conventions Used
Code Samples
Code in this book is displayed using a fixed-width font, like so:
A Perfect Summer's Day
It was a lovely day for a walk in the park.
The birds were singing and the kids were all back at school.
Where existing code is required for context, rather than repeat all of it, ⋮ will be displayed:
function animate() {
⋮
new_variable = Hello
;
}
Some lines of code should be entered on one line, but we’ve had to wrap them because of page constraints. An ➥ indicates a line break that exists for formatting purposes only, and should be ignored:
URL.open("http://www.sitepoint.com/responsive-web-
➥design-real-user-testing/?responsive1");
You’ll notice that we’ve used certain layout styles throughout this book to signify different types of information. Look out for the following items.
Tips, Notes, and Warnings
Hey, You!
Tips provide helpful little pointers.
Ahem, Excuse Me ...
Notes are useful asides that are related—but not critical—to the topic at hand. Think of them as extra tidbits of information.
Make Sure You Always ...
... pay attention to these important points.
Watch Out!
Warnings highlight any gotchas that are likely to trip you up along the way.
Chapter 1: Getting the First Version of the App Up and Running
This book contains a series of chapters on building a todo app with Angular 2+. In each chapter, we'll refine our app, which will look like this:
Angular CLI: The Finished Todo ApplicationBy the end of this series, our application architecture will look like this:
Angular CLI: Application Architecture of Finished Todo ApplicationThe items that are marked with a red border are discussed in this chapter, while items that are not marked with a red border will be discussed in later chapters.
In this first chapter, you’ll learn how to:
initialize your Todo application using Angular CLI
create a Todo class to represent individual todos
create a TodoDataService service to create, update and remove todos
use the AppComponent component to display the user interface
deploy your application to GitHub pages
So let’s get started!
Initialize Your Todo Application Using Angular CLI
One of the easiest ways to start a new Angular 2 application is to use Angular’s command-line interface (CLI).
To install Angular CLI, run:
$ npm install -g angular-cli
This will install the ng command globally on your system.
To verify whether your installation completed successfully, you can run:
$ ng version
This should display the version you’ve installed:
angular-cli: 1.0.0-beta.21
node: 6.1.0
os: darwin x64
Now that you have Angular CLI installed, you can use it to generate your Todo application:
$ ng new todo-app
This creates a new directory with all files you need to get started:
todo-app
├── README.md
├── angular-cli.json
├── e2e
│ ├── app.e2e-spec.ts
│ ├── app.po.ts
│ └── tsconfig.json
├── karma.conf.js
├── package.json
├── protractor.conf.js
├── src
│ ├── app
│ │ ├── app.component.css
│ │ ├── app.component.html
│ │ ├── app.component.spec.ts
│ │ ├── app.component.ts
│ │ ├── app.module.ts
│ │ └── index.ts
│ ├── assets
│ ├── environments
│ │ ├── environment.prod.ts
│ │ └── environment.ts
│ ├── favicon.ico
│ ├── index.html
│ ├── main.ts
│ ├── polyfills.ts
│ ├── styles.css
│ ├── test.ts
│ ├── tsconfig.json
│ └── typings.d.ts
└── tslint.json
If you’re not familiar with the Angular CLI yet, make sure you check out The Ultimate Angular CLI Reference.
You can now navigate to the new directory:
$ cd todo-app
Then start the Angular CLI development server:
$ ng serve
This will start a local development server that you can navigate to in your browser at http://localhost:4200/.
The Angular CLI development server includes LiveReload support, so your browser automatically reloads the application when a source file changes.
How convenient is that!
Creating the Todo Class
Because Angular CLI generates TypeScript files, we can use a class to represent Todo items.
So let’s use Angular CLI to generate a Todo class for us:
$ ng generate class Todo --spec
This will create the following:
src/app/todo.spec.ts
src/app/todo.ts
Let’s open up src/app/todo.ts:
export class Todo {
}
Next, add the logic we need:
export class Todo {
id: number;
title: string = '';
complete: boolean = false;
constructor(values: Object = {}) {
Object.assign(this, values);
}
}
In this Todo class definition, we specify that each Todo instance will have three properties:
id: number, unique ID of the todo item
title: string, title of the todo item
complete: boolean, whether or not the todo item is complete
We also provide constructor logic that lets us specify property values during instantiation so we can easily create new Todo instances like this:
let todo = new Todo({
title: 'Read SitePoint article',
complete: false
});
While we’re at it, let’s add a unit test to make sure our constructor logic works as expected.
When generating the Todo class, we used the --spec option. This told Angular CLI to also generate src/app/todo.spec.ts for us with a basic unit test:
import {Todo} from './todo';
describe('Todo', () => {
it('should create an instance', () => {
expect(new Todo()).toBeTruthy();
});
});
Let’s add an additional unit test to make sure the constructor logic works as expected:
import {Todo} from './todo';
describe('Todo', () => {
it('should create an instance', () => {
expect(new Todo()).toBeTruthy();
});
it('should accept values in the constructor', () => {
let todo = new Todo({
title: 'hello',
complete: true
});
expect(todo.title).toEqual('hello');
expect(todo.complete).toEqual(true);
});
});
To verify whether our code works as expected, we can now run:
$ ng test
This executes the Karma test runner and run all our unit tests. This should output:
[karma]: No captured browser, open http://localhost:9876/
[karma]: Karma v1.2.0 server started at http://localhost:9876/
[launcher]: Launching browser Chrome with unlimited concurrency
[launcher]: Starting browser Chrome
[Chrome 54.0.2840 (Mac OS X 10.12.0)]: Connected on socket /#ALCo3r1JmW2bvt_fAAAA with id 84083656
Chrome 54.0.2840 (Mac OS X 10.12.0): Executed 5 of 5 SUCCESS (0.159 secs / 0.154 secs)
Example Code
If your unit tests are failing, you can compare your code to the working code on GitHub
Now that we have a