loading...
Cover image for The introduction to Angular you may be missing

The introduction to Angular you may be missing

michaeljota profile image Michael De Abreu ・7 min read

What is Angular

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.

Single page application architecture

Every application have its own architecture, but there are certain parts that all single page applications would eventually need.

  • User Interface Layer
  • HTTP library
  • Logic
  • Router
  • 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

  • View
  • Services
  • Store
  • Domain

You can see the relation between both approaches.

What makes Angular a framework?

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:

  • Animations for javascript animations
  • 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...

Learning Angular

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:

  • Typescript
  • ES2015 (and ES2016, ES2017, ES2018)
  • Decorators
  • Observables
  • rxjs
  • Lazy Loading
  • AOT
  • 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.

Typescript

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+
  • Decorators
  • IDE-like experience

ES2015+

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.

Decorators

The decorator pattern is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.ref In Javascript, Decorator proposal was in Stage 1 when Angular Team announce they will be using decorators in ATScript, a short lived language that extends Typescript. Microsoft accepted to introduce many of ATScript features in Typescript, to be used by the Angular Team in the development of Angular 2. It is now in Stage 2, but its specification have change. Typescript still use the previous specification, as it is used by Angular.

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.

Observables (Reactive Programming and rxjs)

This actually should be the harder thing to learn, but one of the things that you won't live without afterward. Reactive programming is a programming paradigm to deal with data in an asynchronous fashion, that allows to get the last value of a computed operation. Reactive programming implementation in Javascript are going to be Observables currently are on Stage 1, soon to be on Stage 2. This means that in a couple of years, probably, all major evergreen browsers will support this natively.

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.

Lazy loading, AoT, Dependency Injection... And the Angular Style Guide.

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.

ngrx

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.

Developing an Angular application

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.

What's next for Angular?

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.

That's all folks!

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.

Discussion

pic
Editor guide
Collapse
danielfrugoni profile image
DanielFrugoni

Mix Angular 7 with Bootstrap, for me is a winner. I do that, works great and looks good !

Collapse
michaeljota profile image
Michael De Abreu Author

I have worked with both, but sometimes it still feels kind of tricky because Bootstrap stills depends upon jQuery officially. You have some community Angular components, witch is nice.

Collapse
danielfrugoni profile image
DanielFrugoni

I use Material too, but need some works from Angular, I asume next version will come with nice ideas from them.

Collapse
rksmcts profile image
Ravi Kant Singh

How to get value from one component to another component.

Hey I'm new to Angular but great with DOTnet.As we make SESSION in the ASP.net and that SESSION we can fetch at every page, similarly is there any things thru which we can retrieve value on page.

eg. Like LOGIN page, the person ID who logged we can get it any page.

tags: Angular

Collapse
michaeljota profile image
Michael De Abreu Author

You could use either input properties, or a service to manage the application state. A single service maybe good if your app is really small, but if your app gets larger you may want look into ngrx. That is an implementation of Redux for Angular, really well done and it integrates very well with Angular.

I wrote a little example of the first approach here:

Also, I wrote an opinionated style guide about ngrx:

Collapse
cristiano profile image
cristiano

The notes on decorators and observables were really helpful to me in understanding them. Thank you for sharing this. 🙇🏻‍♂️

Collapse
hashimebrahim profile image
Hashim Maliakkal

Wonderful article!
Good one!

Collapse
jeexan2 profile image
Jysan Aziz

That's really an appreciable writing about angular. Good one!

Collapse
aarondrs profile image
Aaron Rodríguez

Really good job explaining Angular.

Collapse
suvik023 profile image
Suvendu Karmakar

It was a good read. Great job !. Although I would've benefited more , had it included a more detailed section about state management.

Collapse
michaeljota profile image
Michael De Abreu Author

Thanks! I agree, there should be more about how to manage the state in Angular, but there are several approaches and almost all of them are using services at the end.