Getting Started With Angular JS
All rights reserved.
Chapter 1: Getting started
MVC (Model-View-Controller), 11
Angular Architecture, 12
Angular JS advantages, 15
Comparison with JQuery, 16
Angular movie app review, 19
Angular skeleton project, 46
Download code, 46
CHAPTER 2: Controllers & Markups
Controllers & Scope, 48
Writing first controller, 49
Displaying Repeating information, 57
Handling events, 61
Built-in directives, 70
Event directives, 70
Other directives, 71
Custom filters, 91
Two way binding, 92
CHAPTER 3: SERVICES
Creating Custom service, 105
Creating user page, 108
Built-in services, 121
Using $http & $q service, 122
Angular js graph, 129
Using $resource service, 130
Using cache factory service, 136
Using compile service, 141
Using parse service, 145
Using locale service, 146
Exception handler service, 149
Filter service, 151
Creating auth service, 153
CHAPTER 4: Routing
Adding first route, 165
Adding more routes, 170
Parameterized routes, 173
Default route, 181
Using location service, 182
CHAPTER 5: CREATING CUSTOM DIRECTIVES
Creating first directive, 185
Encapsulating elements, 189
Isolating directive scope, 194
Handling events, 201
Using controllers, 202
Using require, 205
Directive priority, 207
Nested directives, 208
CHAPTER 6: TESTING ANGULAR
Installing karma, 211
Webstorm settings, 216
Testing controllers, 222
Testing service, 227
Writing ajax test, 228
Writing filter tests, 230
Writing end-to-end tests, 232
Debugging tips, 237
CHAPTER 7: GETTING STARTED WITH ANGULAR2 & TYPESCRIPT
Creating New Project, 258
Creating Components, 259
Embedding Components, 260
Using Babel, 261
Using TypeScript, 263
Using TypeScriptWith Angular, 265
Configuring TypeScript in WebStorm, 268
Configuring File-Watcher, 269
Creating Angular App, 274
Visual Studio Code Glimpse, 279
About the author, 290
Angular JS fills this gap very nicely by providing a wrapper around JQuery on the top of DOM especially in the areas of writing boilerplate code and making application more maintainable, reusable and testable. Angular JS been written by keeping testing in mind. Hence, it ensures that each and every piece of Angular JS is testable. Angular JS fits in all the scenarios where in basic stuffs like Data-Driven programming, Declarative programming, and Modular programming is required. Angular JS is evolving very rapidly. Their development team and community are adding tons of useful features to this library to make the same more robust and useful.
Who This Book Is For
FROM THE MANAGER’S DESK
Today, no web developer worth his salt can afford to ignore learning concepts and tools required to develop SPA applications. There is a need today more than ever before to develop applications faster, modular, so large distributed teams with diverse skill sets can work in parallel to create an application that can provide the best of user experience and yet be performant, maintainable, extendable and testable. AngularJS is the framework to go to achieve all the above goals.
This book is an excellent resource for anyone beginning to learn Angular. Rahul has been extremely diligent in coming up with examples that are real world like and yet do not take away the reader’s attention from angular concepts he is trying to explain. The concepts are first clearly explained followed by code for building a moderately complex application. Each area of Angular is thoroughly dealt with including the tools in Angular eco system such as WebStorm, Visual Studio Code, Karma, and Yeoman. Overall, it is an enjoyable and informative read.
AngularJS has come a long way since its inception in 2009. Its initial success was spectacular when it helped to reduce 17,000 lines of code to 1,500 lines of code at Google in the Google Feedback project. Hevery, a developer in the Google Feedback project, could rewrite the code that was written over 6 months in flat 3 weeks using the AngularJS framework. Moreover, the product that was built in 3 weeks was also of a better quality in terms of ease of testing. This initial success of this platform made many Googlers support continuous development of this open source framework.
This book has done an excellent job in introducing this able platform to software developers. Rahul Sahay, the author has built an example web application, which has helped to reinforce the concepts of the project through practical implementation. The book will serve as a good tutorial and a good reference for all the developers who want to leverage the enormous capability of the AngularJS framework. With Single Page Applications and other associated frameworks such as Node.js getting popular and popular, this book is being released at an apt time. Enjoy the technical feast!
How this book is structured
Chapter 1, Getting Started
Starts by explaining what is Angular, why it is needed? How it fits in web application. You will also learn the overview of Angular JS. Then, you will understand how to build Single Page App.
Chapter 2, Controllers & Markups
In this chapter, you will learn scope and controller, which is the nerve of any ng app. Then, writing your first controller. Here, you will be using different ng pieces to display the info on the page via controller.You will also see directives, filters, and expressions in action. Then, in the end, you will be able to do client side validation for the page.
Chapter 3, Services
Services are very important ingredient in angular application. You will begin this section, by learning what are services, how do you create them and then how to register the same with angular app. After that, you will be using some built-in angular services and writing custom services for specific scenarios. Here, I have also introduced Angular JS Graph to visualize the app in console.
Chapter 4, Routing
Routing is the place, where you actually convert your angular app into Single Page App. You will learn how to configure the routes. Then, you will also learn how to configure Parameterized routes. Last but not the least you will be using location service with routes.
Chapter 5, Creating Custom Directives
In this section, you will learn how to get started with custom directives design. Why do you need them in any angular app? How it enhances the reusability and maintainability of the code? You will also learn how to encapsulate elements. Then, you will learn how to isolate the scope from the parent scope and many things that are more relevant which is required on day-to-day basis for any angular app.
Chapter 6, Testing Angular
This is the last chapter of this book where in you will learn how to get started with Angular Testing. First, you will learn how to setup the dev environment to launch the Karma Test runner. Then, you begin this section by writing simple controller tests. Afterwards, you will be learning how to write different kinds of tests and in the end, there will be a bonus section, where in you will learn how to inspect and troubleshoot your angular app.
Chapter 7, Getting Started With Angular 2 & TypeScript
This is the last chapter of the book where you will get the feel of upcoming Angular 2.0 version. Here, you will get started with new angular features. Then, we will see how to get started with angular 2.0 with simple demos using Typescript, ES6 and ES5 as well. After understanding TypeScript basics, we will build one small Movie-Review version with TypeScript & Angular.
Again, this book would not have been possible without the loving support of my wife Nivi, who had to take over much of the household responsibility apart from her teaching activities. Appreciation also goes out to my Mom and Dad for believing in me and always keeps on encouraging me to complete the book in a best possible way. Moreover, I would also like to thank Sita and Piram for sharing their views on the book. In addition, I would also like to thank Arun and Mayank for doing the technical review and suggested few changes around it.
In the end, you know how it is, you pick a book and flip to Acknowledgement’s page and find that author has once again dedicated the book someone close to him, not to you. Not this time. I would like to thank all the readers whole-heartedly for choosing the book. Finally, I would like to thank readers of my blog (http://myview.rahulnivi.net). Many of you have contributed by asking questions, providing feedback, and inspiring and encouraging me in everything, I do.
Chapter 1: Getting Started
WHAT DO you find in this CHAPTER?
- MVC (Model-View-Controller)
- Angular Architecture
- Angular JS Advantages
- Comparison with JQuery
- Angular Movie App Overview
- Angular Skeleton Project
- Download Code
The Core concept behind Angular JS is MVC Architectural Pattern. MVC stands for Model-View-Controller. MVVM (Model-View-ViewModel) also similar to MVC, just a design pattern to separate the units of responsibility in different containers. This kind of design pattern gives developers a sense of architectural thinking that how they want to separate the different layers of their web application. MVC design pattern splits the app into three distinct pieces. They are-
- Model: – Model is nothing but data repository for the application usually fetched from the server and served on the web page via controller. Hence, any web application that is not static, which is getting data is coming via model only.
- View: – View is nothing but the presentation layer of web application. You can also think view as generated HTML. Views are basically dynamic in nature as it entirely depends on the data being fetched from the server and how it is finally presented.
- Controller: – Controller is the central processing unit of any app that is based on MVC design pattern. Once user requests the page, that is getting intercepted by controller and then controller decides where to pick the data from and finally pick which template.
As a result, each unit is responsible for one and only thing. Model means data, View means UI and controller is the business logic. Moreover, each unit is independent of each other, which makes angular more robust, unique and easy to maintain.
In this section, we will learn more about Angular JS architecture. However, before discussing angular architecture, let us discuss conventional web application architecture in brief. As you can see in the below diagram, whenever you make a request it is going to server and fetching the entire asset time and again which is very resource intensive, hence puts pressure on the bandwidth, on the server and end result is site response time is slow.
However, in case of angular, it loads all the assets and required components in the first load only. Then, whenever next request comes for any different link on the page, it only replaces JSON there. This way, angular makes any app very lightweight and fit for any device.
One more point to note here that modern day apps needs that universal architecture support which means you write once and use everywhere like shown below.
Angular js advantages:-
In this section, we are going to discuss the Angular JS benefits. However, from the previous section, you might have got the glimpse of benefits of angular.
- Since, there are many things already built in Angular JS framework, hence it needs only proper API implementation to get the functionality running rather than using JQuery.
- As, I said, Angular is built on the top of MVC design pattern. Hence, it helps keep your code modular, maintainable, reusable and testable.
- Angular JS declarative nature gives a snapshot to developer by just looking at the code that what would have been intention behind writing this code.
- Angular JS supports many third party libraries, which people need as to style the app, notify the users and many other use cases that people need on day-to-day basis.
Comparison with Jquery:-
In this section, we will do a brief comparison between JQuery and Angular JS. I have mentioned detailed side-by-side comparison between the two in below table.
|Abstract The DOM||Y||Y|
|Cross Module Communication||Y||Y|
|Integration Test Runner||N||Y|
|Unit Test Runner||Y||Y|
However, JQLite is already built-in in angular. Hence, you do not need to explicitly use JQuery for any DOM manipulation. You can use JQuery stuffs by calling like angular.element().
Modules in Angular JS are one of the key reasons for keeping application modular. Below is the simple diagram of modules briefly.
As you can see in the above screen shot, modules let you create all underlying pieces. All these pieces we will understand piece by piece in coming chapter. One more point to understand an angular application can have as many modules as required and all of these are dependent on each other like shown below.
Hence, if your app module is dependent on some other module, then that dependent module can be injected before executing the actual app module. We will see all these things in detail in coming chapters.
- Visual Studio
- Visual Studio Code
- Sublime and many more
Angular movie app review:-
I always like talking about the application, which you will be building here, with app snapshots, first. Below is the home page of the application.
Now, when you click on any of these movies, it will take you to the corresponding pages as shown below-
Similarly, you can post new movie like shown below.
You will also learn how to write client side validation like shown below.
Moreover, when everything goes well, then Post Movie button will enabled as shown below.
As you can see that, once image URL pasted there, it produced the relevant image. This is called data binding, which you will learn in coming chapters. Then, when you click Edit profile, it will produce the below screen.
Here, you will learn the power dynamic data binding on the fly using Angular JS. Like, when I type my email id in email address box, it will fetch my image as shown below.
Similarly, when you click on the last link, then this will produce the below page.
Hence, let us suppose if we enter values as shown below in the screen shot, respective cache size value also getting changed.
Then, you can retrieve the same by providing key as shown below.
Similarly, you can delete the same based on key or flush entire set. We will discuss all these terminologies in coming chapter. We will go through complete list of directives we generally use on day-to-day basis. However, one directive that I used recently in one demo wanted to show here. This directive is used for embedding you tube videos in your angular application, angular way. Below is the glimpse for the same.
I have also explained normal authentication service by enabling user to do registration on the site and then login accordingly as shown below.
Apart from this, I have also explained how to get started with yeoman scaffolding template generators for angular and other required dependency. You will learn the same in coming chapters. However, below is the short glimpse for the same.
After doing improvisation on the generated code, app will look like as shown below.
I have also discussed about Angular2 Features in the last chapter. Then how start with TypeScript and write the angular app using that. Below is the glimpse of the same.
This also produced me the required output at the bottom of the screen in JSON format.
Then, how to get started with TypeScript with simple TypeScript workflow discussion.
I hope you have enjoyed the app glimpse. Hence, without wasting time let us get started.
Angular skeleton project:-
In order get started with Angular JS, you can download the skeleton project from here (https://github.com/rahulsahay19/Movie-Review-Angular-Skeleton) , if you want to code along with me on the same study. However, it is not necessary to follow this convention. You can surely apply the angular techniques in any angular project that you want.
You can also download the full finished version from here (https://github.com/rahulsahay19/GettingStartedWithAngularJS).
In this chapter, we have started with Angular JS overview. We started with basic understanding of angular then we also had a glimpse of MVC structure on which Angular is laid upon. Then, we discussed angular architecture in detail by comparing the same with conventional web-app architecture. We have also done concise comparison with JQuery, in order to understand how Angular JS fits in modern day web architecture. Then, we summarized this chapter by giving brief overview of the application, which you will be building along.
Buy Here: – http://bit.ly/ng-kindle
Thanks for Joining Me!
4,338 total views, 1 views today