Angular is a framework to develop Single Page Applications, based on TypeScript. It is developed by the Angular Team in Google, and was launched in 2016 with a stable version 2. It is a complete rewrite of AngularJS, another framework developed by Google.
Angular use common features from server side frameworks, such as Modules, Dependency Injection, a typed language, and recommends a folder-by-feature structure, making several decisions for you, including design and architectural decisions. This things makes a bit harder to enter to Angular development, and you may find you self looking for the Angular way when doing the most simple thing. But they also allows a easy scaling of the application, and a consistent development in a large team of developers.
Every application have its own architecture, but there are certain parts that all single page applications would eventually need.
- User Interface Layer
- HTTP library
- State manager
This are, somehow, the most common pieces that you would need to build a complete SPA.ref
Another way to look at a SPA architecture can be through layers: ref
You can see the relation between both approaches.
If you take a look a the architecture, you will notice that Angular actually have a module, or library, for each part of the architecture.
This is what makes Angular a framework, the fact that it is composed by several modules that fills the needs to develop a Single Page Application.
In comparison with React, or Vue, they only represents the View part of the architecture, or the User Interface, leaving you on your own to choose the best library for the another layers of the application.
- Components for the User Interface Layer
- HttpClient as the HTTP library
- Component and Services for the Logic
- Router for the Routing
- Services as State manager
Additionally you have other libraries to help you in the development of the application, such as:
- Forms for template driven forms
- ReactiveForms for model driven forms
- i18n for internalization, and localization
- Pipes for template data transformations
- Platform modules for different target platforms (eg: Browser, Webworker)
- Guards for authentication and authorization management
- CLI tool for fast setup, testing and build
- Many more...
Angular have a
s shaped learning curve, meaning that you probably will start really slow, as you need to learn how to use several tools aside from the framework itself that is actually quite large and full of features. You will need some time to have experience with it, but after that, you probably wont need to learn anything else.
To start with Angular, there are several technologies that may afraid you at first:
- ES2015 (and ES2016, ES2017, ES2018)
- Lazy Loading
- Dependency Injection
- And list goes on...
It's totally normal to feel afraid by those, I mean... What the heck Angular? Why can't you just be normal?
And... the Angular Style Guide.
This design choices are valuable when developing a large application, but they will benefit you even in a small application.
If you want a complete introduction to the Angular world, the Tour of Heroes tutorial is the place to go. It is constantly updated, and will have the most used feature covered in a really step-by-step explanation.
Now I will give a small introduction of the most important, and most controversial tools used by Angular.
This is the most common argument against Angular, and actually you can develop an Angular application without using Typescript. But, here is why you probably will better with it:
- Automatic Dependency Injection
- Advantages of ES2015+
- IDE-like experience
Some of us are still using plain ES5 to develop an application. Well, almost ES5, as we always are using something on top of it, as lodash, jquery, and other things. But build tools, as Webpack or Rollup, are now quite common, and you should not be uncomfortable using them. Using features of ES2015+, you probably won't even need jQuery or lodash.
The most important thing to understand about Decorators, is that at the end, they are only functions, that add a behavior in a class, property or method. They are really easy to use, to learn, and to develop. If you are have a React background, you can think they are some kind of high order function.
rxjs is a library that implements its own version of the Observable, and it's used heavily by Angular, specially to deal with data fetching as HttpClient instance will return an observable, and to emit events, as EventEmitter extends from Observable.
To make your learning path easy, every instance of rxjs Observable have a
toPromise method, to resolve the value of the observable as a promise after it completes. This is tricky when you are dealing with user events, as most of then will never complete, but you can use this approach to deal with data fetching, as they will be complete after the request. Still it's better to understand rxjs observables, and how to use its operators.
Dependency injection is something that you should need to know when developing with Angular, but this is somewhat common in other frameworks, specially in server side frameworks. Still, if you don't know what dependency injection is, very quickly, is the ability to ask for an instance of a class, and have it. This a really simple way to see it, but at the end, that's actually what it is.
For everything else, you have MasterCard. Ok, you may not. But don't need to know all this things. Sure, lazy loading and AoT will help you to reduce the bundle size of the application, but first, build that application. It's easy to add up those things afterward, and they are not needed to start developing.
About the Angular Style Guide, well, that's something I like about Angular, they have it's own way to do things. When you have time, give it a look. Sure it will help you when dealing with a large application.
I will mention
ngrx as this is the only state library, in my own opinion, that actually have a deep integration with Angular. This is really easy to use, after you understand the flux pattern. I even learn this by doing a React+Redux course in EggHead. So, yeah. The how to use is Redux.
I will cheat here, and will use Angular-cli.
$ npm install -g @angular/cli $ ng new demo-app
This will generate all the necessary files to develop an Angular application, and even setup Karma as unit testing framework, and Protractor for the e2e testing. To start the new application, I will use Angular CLI again.
$ npx ng serve // or $ npm start
Angular-CLI creates several scripts in package, to serve, build, and test.
This will serve the Angular application using a Webpack Dev Server underneath. The Angular-CLI is a super powerful tool, that will allow you to create new projects, components, services, pipes, models, and almost anything you will need to develop an Angular application.
You have to notice that using Angular-CLI have a downside, you can't configure directly the build configuration. This is a design decision, and it does have a motive, but still. If you want complete control over the build process, that you should not want because the CLI will do almost everything for you, but if you want, you just need to
ng eject the project.
Using Angular-CLI does not stop you to integrate the Angular application with other frameworks as Electron, NativeScript, or Ionic.
So far we have see several key features that Angular have, but there are many things to come. Some of the following features are now partially available or in a beta version, some of them will arrive in the next version of Angular, and some of them will be following after that.
- Angular Elements - Use Angular Components as Custom Web Components
- Ivy Renderer - Reduce the bundle size of the Angular application, and increase the rendering performance.
- Component Dev Kit (CDK) - High performance set of Angular components to include with your CSS framework of choice.
- Bazel - A better build tool.
- Schematics - File code transformations for Angular CLI projects.
Of curse, the road-map also includes several optimizations and bug fixes, but this are the new features that you will be using in your next projects.
Thanks for reading. I hope you like it. Share it with your friends, your coworkers, your PM and let me know what you think in the comments.