-
Notifications
You must be signed in to change notification settings - Fork 1
Angular basics
Angular is what we call a "Single Page (Web) Application" (SPA).
Classical (static) web applications work like follows:
- You make an HTTP query to a server from a client (i.e. from a web browser) ;
- The server sends back HTML code (and css and other resources) ;
- The client renders the page using the code it got from the server.
Classical (dynamic) web applications work like follows:
- You make an HTTP query to a server from a client (i.e. from a web browser) ;
- The server generates an HTML document using data (usually found in a database) and sends to result back ;
- The client renders the page using the code it got from the server.
Single Page Applications work like follows:
- You make an HTTP query to a server from a client (i.e. from a web browser) ;
- The server sends back an HTML document with js code and other resources ;
- The client renders the page using the document and runs the js code ;
- The js code uses AJAX (async HTTP queries) to fetch data from the server and display it dynamically on the page ;
- When the user wants to change page, the js code fetches the new page async. and replaces a part of the page with the new one.
The last point is the reason why it is called a Single Page Application (the page is loaded only once from the browser's perspective).
Imagine you have an application with a header (i.e. logo and menu options) which is present on every page. In a classical web application, every time the user would change page, you would have to include the header in the page and return it with the page. In a SPA, you only need to send the header once and when the user changes page, you can just replace the main section of the webpage.
Angular is a SPA framework developed by Google inc.
Two massively different versions have seen daylight : AngularJS and Angular 2+. AngularJS was the first version of Angular and it was completely written in JavaScript. The whole architecture has been rethought for Angular 2 and subsequent versions are improvements upon this architecture. Angular 2+ is written in TypeScript (thus later transpiled to JavaScript).
To develop an Angular project, you will need the Node.js package manager (npm) and the Angular CLI (command line interface).
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -sudo apt-get install -y nodejs
npm install -g @angular/cli
Angular is a component based framework. Every interface element is first defined as a separate component, then included in an other component. Obviously, there is a root component from which the page is defined as a tree.
You can generate a component using the Angular CLI: ng g c [name of the component] or ng generate component [name of the component].
You may get an error message saying it could not determine in which module to create the component.
If that happens, use the : ng g c [name of the component] --module app command instead.
Once you have created a component (i.e. ng g c demo), you will get a new folder (i.e. demo) with three files in it:
- The view (i.e.
demo.component.html) ; - The code behind (i.e.
demo.component.ts) ; - The stylesheet (i.e.
demo.component.cssor other extensions depending if you are using .scss, .less or whatnot) ; - The tests specifications (i.e.
demo.component.spec.ts) -> unit tests will be detailed in further publications.
// To be continued