What makes Angular so powerful?

There are a lot of factors behind the growing popularity of Angular. One of those being the more and more realization of the potential of Single-Page Applications.

I believe there are many reasons why someone may choose Angular for the development process. In What to learn in Angular? we discussed the wide range of features available in Angular and all of these contribute to its usefulness. However, in this post, I am picking out 3 things.

TypeScript

JavaScript though very popular and useful, being a weakly typed language may bring in some natural sources of errors. Someone who is much into a properly organized form of programming may sometimes feel a little bit uncomfortable with the way weakly typed languages handle the data types. And we need to be a bit extra cautious indeed while working with them as they do can let the programmer introduce some common errors in the code (especially runtime errors can get difficult to debug sometimes).
TypeScript facilitates developers to follow that systematic type checking approach. This can help to catch errors at a much early stage and prevent them from reaching the runtime phase.

Encapsulation

I would say Angular is an Encapsulation marvel. It's almost everywhere. Most of us come across encapsulation as a fundamental concept of Object-Oriented Programming (OOP). But, usually, we tend to miss the realization of the concepts like Encapsulation, Abstraction, etc. in the practical world.
Maybe Angular can help in that realization!

Encapsulation in its simplest form can be understood as "To put the related things together." For example- A "Class" puts together various methods, properties, etc. which tend to be related or used together in some way.

Now, let's think about Angular. It uses Encapsulation top to bottom. 
Components- They bundle a certain section of the UI together- be it their template (HTML), or their stylings (CSS/SCSS), or their logic(Typescript). This is what makes a component.

Furthermore, related components, pipes, directives, etc. all can be bundled together using modules.

There is 1 more thing worth noticing. The CSS codes you write for one component doesn't have an effect on the other components. This might be taken for granted by many of us and we may argue- "Of course they don't as they are different components."

But, think about it. As I mentioned in What is a Single-Page Application?, an Angular app is ultimately a Single Page Application which effectively means that everything is on a single HTML page (all the components). So, a CSS applied for a particular section of that HTML page not messing up with the other sections (even if we use the same CSS selector in different components) is definitely not a coincidence. This is being possible only because Angular strongly implements Encapsulation for its views which makes things so intuitive for us.

To see this particular thing that I just mentioned in action- you may override this default behavior that Angular offers. Just use encapsulation: ViewEncapsulation.None in your component.ts file and you will soon find your CSS leaking into the other components. This may be verified through the Inspect window.

Inbuilt Routing

The routing in Angular forms a crucial part of the Single Page Application. Many times we may miss the significance of routing provided in Angular as we may think that our simple old HTML was also able to do this through anchor tag and href attribute.
In reality, however, these two are very different things. And the difference again here is similar to what we had discussed in What is a Single-Page Application?

When we use a href we essentially reload the whole app whenever that link is clicked. This causes the app to lose its state. On the other hand, while using the router link provided by Angular, we just tell Angular which view we want to navigate. It calculates the URL on our behalf and then the running javascript displays or hides the corresponding views. And these two approaches may appear the same on the surface but they are poles apart in reality. The latter approach gives us the fluid experience that we usually expect from a SPA.
Ideally, an Angular app once launched should never reload itself (unless the user explicitly reloads the page).

For more insights, watch this video-

Comments

Popular posts from this blog

What are Bootstrap breakpoints?

What is Bootstrap Grid System?

What is Bootstrap? Why use it?