Angular 5 Basic Demo Project Overview

Table of contents
  • Introduction
  • Source Code
  • Background
  • Understanding Angular Application
  • Conclusion
  • Your turn. What do you think?
Introduction

This post is a continuation of the Angular 5 series and you can find the first part of this series at What Is New and How to Set Up our First Angular 5 Application. So, if you haven’t gone through the first part yet, I strongly recommend you to do that. In our first part, we saw the Angular 5 updates and how to set up our first application in Angular 5. In this post, we are going to discuss a few files and application overview. We will also be discussing some key points like Components, Declarations, Modules, Providers etc. So, at the end of this article, you will have some basic understanding of Angular project structures and how it really matters when it comes to project. I hope you will like this article.

Source Code

You can always clone or download the source code from here.

Background

Though Angular 5 has exactly the same project architecture as the previous version, we will be explaining some key elements so that the beginners can follow up this series. I hope you would not mind if you are an experienced professional. Thanks for understanding.

Understanding Angular Application

The Angular project has a clean architecture. It contains the folder structure, as shown in the preceding image.

Angular_App_Folder_Structure

Angular_App_Folder_Structure

We are going to start developing our application in src/app folder which is our application root folder. For now, let’s go ahead and see what we have got in the existing files. Let us open the file app.component.ts.

  1. import {  
  2.     Component  
  3. } from '@angular/core';  
  4. @Component({  
  5.     selector: 'app-root',  
  6.     templateUrl: './app.component.html',  
  7.     styleUrls: ['./app.component.scss']  
  8. })  
  9. export class AppComponent {  
  10.     title = 'app';  
  11. }  
In Angular, we are creating components for each selected functionalities; let’s say one component for login, and another one for registering, another for navigation etc. so that each can be maintained separately. In each component, we have 3 parts.
  1. Import section
  2. Decoration
  3. Export section

The Import section helps us to import some existing functionalities available in the framework like we have included the component from Angular/core in the above code snippet.

The decoration is the place where we decorate the component, giving a selector( which is unique to each component), Style the UI, giving the template URL which points to a particular HTML file where we customize the component. We can also include our template in our decoration itself as a template: "your custom HTML."

The Export section includes the custom TypeScript code which is specific to that component. Let’s say it is a class which holds its own functionalities.

Now, let us just move our pointer to app.module.ts. This is the place where we register all of our modules, components, providers, etc. We can consider this as a base class. Like components, it has a section for import and export. It also has a @NgModule section which contains our declarations (components), imports (modules), providers (services).

  1. import {  
  2.     BrowserModule  
  3. } from '@angular/platform-browser';  
  4. import {  
  5.     NgModule  
  6. } from '@angular/core';  
  7. import {  
  8.     AppComponent  
  9. } from './app.component';  
  10. @NgModule({  
  11.     declarations: [  
  12.         AppComponent  
  13.     ],  
  14.     imports: [  
  15.         BrowserModule  
  16.     ],  
  17.     providers: [],  
  18.     bootstrap: [AppComponent]  
  19. })  
  20. export class AppModule {}  
I am not going to explain about app.component.css, app.component.html, app.component.spec.ts files as you can see what they have by seeing the code inside. So, I am leaving that to you.

If you see the project folder, you can see a folder named Environment. As the name implies, it helps us to set the configuration for our environments, say, for development and production etc.

Index.html is the main page from which we call the app-root. Remember, we set the app.component’s selector as app-root.

  1. <!doctype html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <meta charset="utf-8">  
  6.     <title>MyAngular5App</title>  
  7.     <base href="/">  
  8.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  9.     <link rel="icon" type="image/x-icon" href="favicon.ico"> </head>  
  10.   
  11. <body>  
  12.     <app-root></app-root>  
  13. </body>  
  14.   
  15. </html>  
Please make sure that you are giving <base href=”/”> in your index.html file, else you will get an error in your Agular app.

The tsconfig.app.json file is the place where we can set the configuration for our project, like setting the module versions, compiler options etc. You can see a sample file as below.

  1. {  
  2.     "extends""../tsconfig.json",  
  3.     "compilerOptions": {  
  4.         "outDir""../out-tsc/app",  
  5.         "baseUrl""./",  
  6.         "module""es2015",  
  7.         "types": []  
  8.     },  
  9.     "exclude": ["test.ts""**/*.spec.ts"]  
  10. }  
If you have created your Angular app using Angular CLI, you will see a file named .angular-cli.json in your project. This is the file where all your custom project settings reside.
  1. {  
  2.     "$schema""./node_modules/@angular/cli/lib/config/schema.json",  
  3.     "project": {  
  4.         "name""my-angular5-app"  
  5.     },  
  6.     "apps": [{  
  7.         "root""src",  
  8.         "outDir""dist",  
  9.         "assets": ["assets""favicon.ico"],  
  10.         "index""index.html",  
  11.         "main""main.ts",  
  12.         "polyfills""polyfills.ts",  
  13.         "test""test.ts",  
  14.         "tsconfig""tsconfig.app.json",  
  15.         "testTsconfig""tsconfig.spec.json",  
  16.         "prefix""app",  
  17.         "styles": ["styles.css"],  
  18.         "scripts": [],  
  19.         "environmentSource""environments/environment.ts",  
  20.         "environments": {  
  21.             "dev""environments/environment.ts",  
  22.             "prod""environments/environment.prod.ts"  
  23.         }  
  24.     }],  
  25.     "e2e": {  
  26.         "protractor": {  
  27.             "config""./protractor.conf.js"  
  28.         }  
  29.     },  
  30.     "lint": [{  
  31.         "project""src/tsconfig.app.json",  
  32.         "exclude""**/node_modules/**"  
  33.     }, {  
  34.         "project""src/tsconfig.spec.json",  
  35.         "exclude""**/node_modules/**"  
  36.     }, {  
  37.         "project""e2e/tsconfig.e2e.json",  
  38.         "exclude""**/node_modules/**"  
  39.     }],  
  40.     "test": {  
  41.         "karma": {  
  42.             "config""./karma.conf.js"  
  43.         }  
  44.     },  
  45.     "defaults": {  
  46.         "styleExt""css",  
  47.         "component": {}  
  48.     }  
  49. }  

Last but not least, package.json is the file where your NPM packages are mentioned, and whenever you run npm install commands, this is the file that the command looks into.

  1. {  
  2.     "name""my-angular5-app",  
  3.     "version""0.0.0",  
  4.     "license""MIT",  
  5.     "scripts": {  
  6.         "ng""ng",  
  7.         "start""ng serve",  
  8.         "build""ng build",  
  9.         "test""ng test",  
  10.         "lint""ng lint",  
  11.         "e2e""ng e2e"  
  12.     },  
  13.     "private"true,  
  14.     "dependencies": {  
  15.         "@angular/animations""^5.0.0",  
  16.         "@angular/common""^5.0.0",  
  17.         "@angular/compiler""^5.0.0",  
  18.         "@angular/core""^5.0.0",  
  19.         "@angular/forms""^5.0.0",  
  20.         "@angular/http""^5.0.0",  
  21.         "@angular/platform-browser""^5.0.0",  
  22.         "@angular/platform-browser-dynamic""^5.0.0",  
  23.         "@angular/router""^5.0.0",  
  24.         "core-js""^2.4.1",  
  25.         "rxjs""^5.5.2",  
  26.         "zone.js""^0.8.14"  
  27.     },  
  28.     "devDependencies": {  
  29.         "@angular/cli""1.5.0",  
  30.         "@angular/compiler-cli""^5.0.0",  
  31.         "@angular/language-service""^5.0.0",  
  32.         "@types/jasmine""~2.5.53",  
  33.         "@types/jasminewd2""~2.0.2",  
  34.         "@types/node""~6.0.60",  
  35.         "codelyzer""~3.2.0",  
  36.         "jasmine-core""~2.6.2",  
  37.         "jasmine-spec-reporter""~4.1.0",  
  38.         "karma""~1.7.0",  
  39.         "karma-chrome-launcher""~2.1.1",  
  40.         "karma-cli""~1.0.1",  
  41.         "karma-coverage-istanbul-reporter""^1.2.1",  
  42.         "karma-jasmine""~1.1.0",  
  43.         "karma-jasmine-html-reporter""^0.2.2",  
  44.         "protractor""~5.1.2",  
  45.         "ts-node""~3.2.0",  
  46.         "tslint""~5.7.0",  
  47.         "typescript""~2.4.2"  
  48.     }  
  49. }  
Once the Package Manager finishes running the install command, all the mentioned packages will be added to the folder node_modules.

In our next article, we will start developing some components in our Angular app. Until then, keep reading and learning.

Conclusion

Did I miss anything that you think was needed? Did you find this post useful? Please share your valuable suggestions and feedback.

Your turn. What do you think?

A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, ASP.NET Forum instead of commenting here.

You can always see this article on my blog here.