This article is based on a alpha version of Angular 2 which is now obsolete. For an up-to-date tutorial, please see the article Angular 2 Tutorial: Create a CRUD App with Angular CLI.
([PDF]) Beginning Angular with Typescript (updated to Angular 6) EPUB by Greg Lim Detail Description Angular is one of the leading frameworks to develop apps across all platforms.
The current stable version of Angular (i.e, Angular 1.x) was built using the features of ES5 and was meant to work on most of the browsers, including some of the older versions of IE. The framework had to create a module system of its own, abstract away some of the language features, and provide a highly abstracted and configuration based interface to work on.
All good things of Angular 1 are still available in Angular 2 but the framework is simpler. Angular 2 is built with features of ES6 (and ES7), Web Components in mind, and targeting evergreen browsers.
TypeScript is a typed super set of JavaScript which has been built and maintained by Microsoft and chosen by the AngularJS team for development. The presence of types makes the code written in TypeScript less prone to run-time errors. In recent times, the support for ES6 has been greatly improved and a few features from ES7 have been added as well.
In this article, we’ll see how to use Angular 2 and TypeScript to build a simple application. As Angular 2 is still in alpha, syntax of the code snippets shown in this article may change before it reaches the stable release. The code developed in this article is available on GitHub.
Basics of Angular 2
Angular 2 was built with simplicity in mind. The team removed a number of recipes of Angular 1 that made us think “Why are we doing this?” (if you want to know what has been removed, I suggest you to take a look at this video titled Angular 2.0 Core session by Igor and Tobias). Now the framework is made of a small set of building blocks and some conventions to be followed.
The building blocks which are present in Angular 2 are:
As in Angular 1, Angular 2 uses Dependency Injection to get references of the objects. As
scope has been removed from the framework, we don’t have digest cycle running. Therefore, we don’t need to keep calling scope.$apply while working in non-Angular world. Angular 2 uses Zone.js to kick the changes and this library knows when to act.
An Angular 2 application starts with a component, and the rest of the application is divided into several components which are loaded inside the root component.
If you want to learn more about the basics of Angular 2, please check Victor Savkin’s blog post about Core Concepts in Angular 2.
Setting Up
Angular 2 is still in alpha at the time of this writing, so the framework and the resources around it are still raw. They’ll go through a number of changes and will get better by the time it will be ready for production.
There is a great number of seed projects to get started with Angular 2 and TypeScript. I think that this one by Elad Katz could be a good starting point to do some practice. To start, if you want to follow along with this tutorial, clone this repository. Then, follow the instructions mentioned in the
readme file to install and run the seed project.
The repository:
How to Pin Your AchievementsBuilding Express APIs
Now that you’ve an idea of what Angular 2 is and you have also cloned the seed project, let’s modify it. We’ll build a simple application to pin your achievements on a board. As the first thing, let’s add the Express APIs to get and add the achievements. As I forked the repository and modified the seed project to add basic Express APIs, you’ll see an endpoint serving the existing list of all the achievements. To post a new achievement, we need to add an endpoint.
To achieve this first task, open the
server.js file and add the following snippet:
As Angular 2’s
Http API is still pretty raw, it posts the data in the form of plain text. So, let’s add a bodyParser middleware to read the text from the request body:
Modifying Startup Component and Routes
The
index.html file is the starting point of the application. The body portion of this file loads the bootstrap script and creates the my-app component. This component in turn loads other components into the application. The bootstrap.ts file bootstraps the AngularJS application using the main component. As you see, the required injectables exported by the other modules are passed into the function. This makes the services and the directives exported by these modules available to all child components of my-app . We’ll be using a form in the application and for that we need to add formInjectables exported by the module angular2/forms to the list of the injectables:
The startup component of the project is located inside the
app folder. The component has two annotations applied on it:
Below, you can see the relevant code:
The
my-app component has to define the routes for the application and provide a placeholder to load the child views. The Router service can be used inside the MyApp class to define the routes. The following snippet defines the two routes needed for the application:
As the
add component has not been added yet, you’ll face some issues if you try to run the application now. We need to create a new folder inside the components folder and name it add . Then, we add two files inside this folder: add.ts and add.html . Finally, add the following snippet to the add.ts file (we’ll add more code later):
The view of this component will have a form accepting inputs to be saved as a new achievement. So, add the following HTML to this page:
In the view, we need to create links for navigating between the pages. The
router-link attribute component helps us in doing this. We need to assign the name of the component to the attribute and it’ll take care of building the link based on the path previously configured for the component.
Angular 2 Pdf
The final piece of the HTML code we need in the root component is a
route-outlet element. This is the place where the child components will be loaded while navigating across the views.
Listing all the Achievements
Now, let’s modify the home component to display a list of all the achievements in the form of pinned boxes on a wall. We’ll use Bootstrap to style this page. Bootstrap’s CSS is already loaded in
index.html from a CDN.
Before working on the UI, let’s create a service to send Ajax requests to the Express.js API to interact with the data. We have a file named
dummyService under the services folder. Rename it as achievementsService and the class inside the file as AchievementsService . Add the following code to this file. This piece of code sets up Dependency Injection for the service and adds a method to get all the achievements from the service:
The methods of the
Http class don’t return promises but observables. The latter are the objects that have built-in capabilities to notify when something changes. The home component needs an object of the AchievementsService and NgFor directive to retrieve and show the list of the achievements.
To move on with the project, replace the code in
home.ts with the following:
The subscribe callback added in the above snippet is called after the observable sends a notification. Zones understand how the observables work and update the UI after the observable has set the value. The markup in the file
home.html is really straightforward and it’s shown below:
Everything in the above snippet looks familiar except two special characters on the
div element containing ng-for . The meaning of these symbols is:
Let’s save these changes and run the application. You’ll see the list of the achievements in the form of boxes.
Adding a New Achievement
We need to have a
form accepting details of an achievement and send these details to the Express server on submission. Add the following method to AchievementsService to post data to the server:
The
add component view will perform the following tasks:
Forms can be created and managed in multiple ways in Angular 2. They can be template driven, model driven and data driven. Discussing more details of these approaches is outside the scope of this article, but in case you’re interested, in this project we’ll use the model driven approach. Though we won’t be using validation in this article, you’ll be happy to know that forms in Angular 2 also support validation.
In model driven approach, we need to create a model object to be bound on the form and declaratively attach it to the form. The fields in the form are bound to the properties of the model object. The value of the model object is passed to the service to send it to the server.
The next step you need to perform is to open the file
add.ts and add the following code inside the Add class:
The property
addAchievementForm on the class has to be used on the form. The properties of this object would be attached to the controls inside the form using ng-control directive. The method addAchievement uses the form model object to pass the value entered on the screen to the server and sends the user to the home screen once it receives a response.
You may have noticed that we’re not handling error cases of the HTTP requests. That is because the feature is not yet available in the HTTP API, but surely it’ll get better in the future.
Now, open the file
app.html and modify the form element as shown below:
In the above snippet, the parenthesis around
ng-submit indicate that it’s an event. The function assigned to it would be called when the form is submitted by the user. The square brackets around the ng-form-model directive indicate the value is bound to a property of instance of the component.
Now the only pending change is attaching fields of the model object to the input controls. The following snippet shows the modified input text box for the title, and modify the other controls accordingly:
To conclude, save all the changes and run the application. You should be able to add new achievements now.
Ke$ha (International) • YouTube ได้รับอนุญาตจาก • WMG (ในนามของ Atlantic Black Music); UMPI, BMG Rights Management, UBEM, The Royalty Network (Publishing), ASCAP, LatinAutor - SonyATV, LatinAutor - PeerMusic, Kobalt Music Publishing, ARESA, Warner Chappell, PEDL, Global Music Rights LLC, CMRRA, SOLAR Music Rights Management, AMRA, LatinAutor, LatinAutor - Warner Chappell, Sony ATV Publishing และสมาคมจัดเก็บค่าลิขสิทธิ์เพลง 16 แห่ง • • แสดงเพิ่มเติม แสดงน้อยลง. • หมวดหมู่ • • เพลง • Right Round (Instrumental) • ศิลปิน • • อัลบั้ม • Right Round Feat. You spin my head right round download.
Conclusions
Angular 2 embraces the current latest and greatest technologies of the front-end world. In addition, by using TypeScript to write code, it boosts up the productivity of the developers. As we saw, the framework is completely re-written and helps you in doing many things in a simpler way. As the framework is still in alpha, I suggest you to not use it in an application that is in production. Be patient and wait to see how the AngularJS team shapes its baby.
Goodreads helps you keep track of books you want to read.
Start by marking “Beginning Angular 2 with Typescript” as Want to Read:
Rate this book
See a Problem?
We’d love your help. Let us know what’s wrong with this preview of Beginning Angular 2 with Typescript by Greg Lim.
Not the book you’re looking for?
Preview — Beginning Angular 2 with Typescript by Greg Lim
Angular 2 is a massively popular JavaScript framework built to take advantage of component development in web apps. In Angular 2, author Joseph D. Booth will guide you through setting up a development environment, interacting with the Angular CLI, building Hello World, and more. By the end, you'll know how to set up templates, compose components from those templates, and t..more
Published July 12th 2017 by Createspace Independent Publishing Platform
To see what your friends thought of this book,please sign up.
To ask other readers questions aboutBeginning Angular 2 with Typescript,please sign up.
Be the first to ask a question about Beginning Angular 2 with Typescript
This book is not yet featured on Listopia.Add this book to your favorite list »
Rating details
Angular 2 Wiki
|
Nov 21, 2017Adam Hansen rated it really liked it · review of another edition
This book is absolutely a great introduction to Angular. The author updates the book, so you always have an up-to-date version, when Angular is updated. Even with knowledge of the framework, the book is still a great glossary to have next to your PC. I would recommend this book if you like learning by reading and want to learn Angular!
Mar 31, 2018Michael North rated it it was ok · review of another edition
Ok as a start, but leans too heavily on firebase.
I wish this book focused on angular itself in greater detail, and not spend so much time working with angularfire and firebase specific concepts.
May 23, 2018Chakravarthy rated it it was amazing · review of another edition
Feb 19, 2018Surya Penmetas rated it really liked it · review of another edition
Good one!
Author did a really good job of gradually building the knowledge with examples that are easy to understand. I recommend!
Jan 25, 2018Brent Thomas rated it it was amazing · review of another edition
Great book for getting the basics down
Could dive deeper into Observables and RxJS but that is outside the courses scope. Ultimately a great book for any skill level js developer
There are no discussion topics on this book yet.Be the first to start one »
Recommend It | Stats | Recent Status Updates
If you like books and love to build cool products, we may be looking for you.
Learn more »
See top shelves…
0followers
“you are a Mac user.” More quotes…
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |