This tutorial on angular 8 directory structure is in continuation of our Angular 8 Tutorial Series and if you have not read the previous part of the tutorial , it is recommended that you go through it first. In the Part-1 of Angular 8 tutorial , we have already created an app called InventionsHub
and in this tutorial , we'll explore the code structure and learn about the different files & directories and their roles in an angular project.
An angular app has a well defined structure , following is the screenshot of directory structure of our InventionsHub angular app. In this tutorial we'll explore this structure in detail.
The screenshot above shows most important files and folders in an Angular 8 app , lets look at each of these one by one.
src is the directory where the actual source for the app lives. Following is a screenshot of the anglar src directory from our InventionsHub app.
now let's try to understand some of the files and folders which we'll be mostly used when we develop our project.
index.html is the landing page or the main page of our application and this is where our angular app bootstraps. As shown in the following code snippet , the app-root selector comes from the angular app as we'll see below.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>InventionsHub</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
</body>
</html>
main.ts is the main typescript file which is used to bootstrap the angular module. The check for environment is also performed here as shown in the following code snippet.
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule);
app directory is where we place the building blocks of our angular app including modules , components , component styles , component templates , services and so on. below is a screenshot of the app directory taken from our InventionsHub app.
Finally , let's look at our app module and the component files.
// app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app'; // this is used in the template
}
<div style="text-align:center">
<h1>
Welcome to {{title}}!! <!-- data inside curly braces comes from angular component -->
</h1>
<img width="300" src="logo.png" />
</div>
<h2>Here are some links to help you start: </h2>
<ul>
<li>
<h2><a target="_blank" href="https://angular.io/tutorial">Tour of Heroes</a></h2>
</li>
<li>
<h2><a target="_blank" href="https://github.com/angular/angular-cli/wiki">CLI Documentation</a></h2>
</li>
<li>
<h2><a target="_blank" href="http://angularjs.blogspot.ca/">Angular blog</a></h2>
</li>
</ul>
{{ }}
curly braces is used for binding component data to template. We'll talk more on this in the upcoming parts of the tutorial series.
Finally we've reached the end of this long tutorial in which we learned about angular project structure. The key takeaways are
src
directory is where we will be writing most of our code