loading...

I had to learn Angular 8 in 24 hours, here is how

evybauer profile image Evelyn Bauer ・10 min read

I had to learn Angular 8 in 24 hours, here is how

Angular is one of the hottest open-source front end frameworks currently in the market. It is used for building client-side web applications and is specifically designed for SPA(Single Page Applications). Released by Google in 2016, the TypeScript based framework was rewritten based on AngularJS. As soon as you start learning Angular you will realize that it is not as simple as learning ReactJS or VueJS, some of Angular’s main competitors. In this article, I’ll explain how I learnt Angular 8 basics in 24 hours in 3 simple steps.

Before we get started, it is crucial to have some understanding of how Angular evolved. It may sound confusing but, before Angular became Angular, Angular was AngularJS. For now, keep in mind that Angular is completely different from AngularJS. If you are not interested in understanding the difference, I suggest skipping the following section.

Differences between AngularJS and Angular

AngularJS was released 6 years before Angular, in 2010 by Google. At that time, it became immediately popular. However, the launch of other frameworks exposed its drawbacks and turned the spotlight away. ReactJS and VueJS arrived on the market and made clear they wanted to compete for the best performance framework. This is when Google’s team realized and decided it was time to rewrite Angular completely. The release of Angular 2 revealed some drastic changes. Language, Architecture and Data binding were the key ones.

Language: JavaScripts vs. TypeScript

Angular team’s first decision was to shift from JavaScript to TypeScript to avoid JavaScript’s pitfalls. Also, a way of presenting a more organized and maintainable code. The static typing of TypeScript improves performance, avoids runtime pitfalls and transpiles to JavaScript.

Architecture: MVC vs. Component-based architecture

AngularJS’s architecture supports the MVC (Model View Controller) architecture. Logic, output and view happen in separate files. On the other hand, Angular relies on components, directives with predefined templates. Such a modern structure allows us to scale and maintain large applications.

Data Binding: Two-way data binding algorithm

AngularJS two-way data binding is a unique way that immediately propagates to the view of any data changes that can affect the model, and changes made by users in views immediately reflect the model. A better Angular algorithm makes it five times faster than AngularJS for data binding and component-based architecture.

HOW I LEARNT ANGULAR 8 BASICS IN 24 HOURS IN 3 STEPS

Before we move forward so I can explain how I learnt Angular 8 Basics in 24 hours in 3 simple steps, it is important to stress that learning Angular takes more than that (much more!). Angular is known for its complexity and long learning curve. However, Angular developers support that this process is worth it since it is a very reliable framework. This post’s purpose is to outline some ideas on how you can break down Angular to have a quick understanding of it in case you have to jump in on a project without experience. Now, we are ready to move forward.

#1 GET FAMILIAR WITH ANGULAR’S STRUCTURE

GET FAMILIAR WITH ANGULAR’S STRUCTURE

When you need to learn a new framework in a short period, the most important is to get familiar with the project’s structure. It may sound useless but you’ll thank yourself for doing that later on. It will save you some time otherwise spent researching irrelevant questions on the internet.

Angular uses some boilerplate that can be easily requested through its CLI. With the help of Angular CLI tools, you can quickly build the projects, set up, add and test components, as well as deploy them. Interestingly, AngularJS does not have its CLI.

Through the CLI, navigate to the directory where you plan to start the Angular project and run the command ng new [project-name]. While it is running, you will be given the option to add routing and selecting the stylesheet format (CSS, SASS, LESS). These options will depend on the type of project you are planning to start. If you do not intend to have routing you can skip the routing option. In terms of the stylesheet, the most common is CSS. After that, the CLI will finish running the commands and will automatically generate a complete project.

Once that’s done, you can navigate to the projects directory and run ng serve, which is the equivalent of npm start to React. The CLI will compile the project and starts a server at http://localhost:4200

Project’s Structure:

At this point, you can open your project on your Code Editor. There you will notice that Angular’s CLI generates a project composed of folders and files. Each folder and file has a different purpose. To make things easier, let’s break them down.

PS: 🗂 refers to a folder and 📝 to a file.

🗂 e2e/
End to end testing folder located at the top level of the project’s structure. It contains source files to perform end-to-end tests to the root-level application (AppComponent).
ng e2e:
When you run ng e2e command on the CLI, the app will be built in watch mode and the Protractor test runner will be launched. Once that is done, you will see the output in the terminal and the chrome browser will open up, test all the scenarios declared in the e2e file and quickly close.

🗂src

🗂app/
It contains the component files in which your application logic and data are defined.

📝 App.component.css
A stylesheet automatically generated to declare styles for the root AppComponent. The extension of the file reflects the style preprocessor you have configured for the project(in this case CSS).

📝App.component.html
A template generated to define the HTML root of the AppComponent.

📝App.component.spec.ts
The file responsible for performing unit tests for the root AppComponent.
ng test:
When you run the ng test command on the CLI, the app will be built in watch mode and the Karma test runner will be launched. The test’s output is revealed in the terminal and a Jasmine HTML Reporter will be shown on a chrome browser which will open up.

📝App.component.ts
Defines the logic for the app’s root component (AppComponent). If you add more components, the app.component.html becomes the root of the views hierarchy.

📝App.module.ts
Defines the root module (AppModule) that tells Angular how to assemble the application. It declares AppComponent by default but can declare more components as you add them to the project.

🗂assets/
A folder automatically generated to store images and other assets files to be copied as you wish to add to your project.

🗂environments/
Contains build configuration (JSON configuration information) options for particular target environments. It is responsible for telling the build system which files to change when you run ng build or ng serve.

📝Index.html — The main HTML page where the views are rendered and are served when users visit your page.

📝main.ts — The main.ts is the main entry point of an Angular application. It uses the JIT compiler to compile the application and bootstraps the application’s root module (AppModule) to run in the browser.

📝polyfills.css — Provides polyfill scripts for browser support.

📝styles.css — All the styles declared here will be globally available. The extension of the file reflects the style preprocessor you have configured for the project(in this case CSS).

#2: UNDERSTAND ANGULAR’S BUILDING BLOCKS

UNDERSTAND ANGULAR’S BUILDING BLOCKS

Every framework varies when it comes to building blocks. It is important to explore some of it with anticipation so you can create a correlation to other frameworks you have used or been exposed before, what they have in common and what their differences are. Angular’s key building blocks are Modules, Components, and Templates. Let’s start with Modules.

Modules

Essentially, a module is described as a block of code with related functions. With that in mind, Angular apps are known for being modular. You can easily identify it by the class NgModules (or the decorator @NgModule). It helps to organize complex relationships between views (classes that can contain view: components, directives and pipes) to data providers. It allows defining all relationships in a single file. Every application must have at least one module or, depending on the size of the application, multiple ones.

To modify JavaScript classes you have to use decorators. They are responsible for attaching metadata objects to classes so the classes know how they should work. In simple words, NgModule is a function that takes metadata objects and the properties to describe the module and organize the relationship.

-- declarations: The classes that are related to views and will be privately available to this module. Think about this as public and private functions in OOP, components are private by default but can become public if you add to exports. If you want them to be publicly available, you’ll have to add to the exports section.

-- exports: The classes that should be accessible to the components from other modules.

-- imports: Other modules have access to exported modules by importing the module to their metadata.

Declaration, exports and imports define the relationship between components and how they are shared between NgModules.

-- providers: Where you register data providers such as services and guards (i.e. ContactService, CanActive, AuthService) that can be injected in the components within this module. The injectable class will be available to any component in this module as well as any module that imports it.

-- bootstrap: Defines a component initially responsible by loading the application (AppComponent by default).

As the app grows, the module will become bigger and harder to maintain at some point. Our job is to refactor components and services to their modules, except AppComponent which we bootstrap from the root module. We bootstrap the AppComponent Module in a main.ts file. The main.ts file automatically specifies the bootstrap module. Finally, the bootstrap component is inside the bootstrap module.

In case you decide to add routers to your project, the router will have its file with import and export metadata only. It should always export the RouterModule. Depending on your app’s structure, it will have CoreModule, FeatureModule and ShareModule but those are topics you can cover in-depth as your project grows.

Components

A component can control one or more sections of a view. The logic of the application must be written inside of the component. The results displayed in the view are generated through a class that interacts with an API of properties and methods.

Adding New Components:

If you wish to add new components to your project, other than the AppComponent, you will have to follow some steps to make sure your project won’t break. You can either add a new component manually or through the CLI. I suggest using the CLI so you avoid unnecessary mistakes that can delay your learning process or the execution of your project.

From Angular’s CLI, access the project’s main directory or the path you wish to create this component and run ng generate component [component-name]. It will automatically create a folder which contains the following files:
-- [component-name].component.ts
-- [component-name].component.css
-- [component-name].component.html
-- [component-name].component.spec.ts
-- index.ts

Additionally, you will notice that it generates a ngOnInit. The ngOnInit method is called always when a component is instantiated. Moreover, you should check some extra details before start working on the new component:

  1. Import components and dependencies to the component
  2. Attach the metadata of the component using @Component decorator.
  3. Specify the logic of the component by specifying the constructor and inside it, the variables and methods that need to be initialized when the class is created.

Templates

The view of a component will always be associated with an HTML file, also known as a template. The HTML tags will tell Angular how to render the component. For those familiar with embedded templating, Angular’s HTML file looks like a regular HTML but can add embedded templating to perform functions to display data, for example for loops followed by curly braces to display the data as the loop goes through.

Data Binding

Two-way data binding combines property and event binding in a single notation, using the ngModel directive. It immediately propagates to the “View” any data changes that can affect the “Model”, and changes made by users in “Views” immediately reflect the “Model”. In simple words, every time the data changes, the UI changes and vice versa. Angular processes all data bindings once per the JavaScript event cycle, from the root of the application component tree through all child components. It plays an important role in communication between a template and its component, parent and child components.

#3 PUT YOUR HANDS ON! EXPERIMENT, TEST, GET COMFORTABLE

PUT YOUR HANDS ON! EXPERIMENT, TEST, GET COMFORTABLE

Now that you have a good grasp on Angular 8 basics it is time to feel how it works and get comfortable with it. As a suggestion, try to follow a simple tutorial or check other open-source projects. After following through these three quick steps, you may not realize but most of what your brain is going to do is to create a comparison to other languages or frameworks that you are familiar with already. This process of making comparisons and assumptions helps you to identify clearly what are some of the advantages and disadvantages

CONCLUSION

Even though Angular’s learning curve is longer than other frameworks, still, this is an amazing framework to learn and spend some time exploring. AngularJS is still in circulation and some developers prefer using it. In case you are wondering which one is the best, the answer will always rely on the type of project you are interested in pursuing. If you envision a mobile-friendly and scalable app, definitely go with Angular because AngularJS does not support it. For simpler purposes, AngularJS would work fine and still be JavaScript friendly.

The challenge of learning a new framework is a painful process in the beginning but worth the effort. Every developer knows that trying something new every day is part of the path. It is not because you do not know a technology that you cannot learn and become proficient in it. Simply go for it, start from the basics, make correlations and, eventually, everything will fall in place. I hope you find this post helpful.

Happy coding!👩‍💻


Sources:
https://bit.ly/2WIIkO4
https://bit.ly/3g5GMFu
https://bit.ly/2LE7TJT
https://bit.ly/2ygUPHf
https://bit.ly/2WNZ06V
https://bit.ly/2zX092L

Posted on by:

evybauer profile

Evelyn Bauer

@evybauer

👩‍💻Full-stack web developer with a desire to develop, create, and optimize user-friendly, feature-rich apps and websites.✨

Discussion

markdown guide
 

Nice article Evelyn. I agree with you in learning the concepts first. Binding concepts are simple. Tests can wait until later. The best way is as you said, spin up a new project and try it out. Learn good debugging skills and it's just a matter of tme. It's nice to see folks taking deep dives in 24 hrs. Looking forward to your upcoming articles.

 

Thanks, @jwp . I'm glad you agree with it. Learning something completely new in 24 hours seems scary, especially for those who are competing for a job position and have to deliver a code challenge within a couple of hours. Looking back now, learning Angular in 24 hours was a stressful experience but totally worth it. In the end, all the interest to dive deep and make the best of it relies on the effort you are putting on it.

 

Your article applies to Angular 9 too.

Stress is the only way to learn, it forces us to dig deep and overcome obstacles.

I predict your job situation will work out soon and with something perfect for you. There are lots of job openings for Angular folks.

I have an article on finding a job in 2020. Please take a look.

That's great! I'm still getting used to Angular 9.
I agree with you, stress gets us out of the comfort zone, which is vital for growth and productivity.
Thanks for the job wishes! Hope to get there soon too :)
I appreciate the suggestion and will take a look at your article for sure.