Angular 6 Tutorial PDF
Angular 6 Tutorial PDF
com
Throughout this Angular 6 tutorial, we'll learn to build a full-stack example web application with Angular 6, the
latest version of Angular — The most popular framework/platform for building mobile and desktop client side
applications, created and used internally by Google.
By the end of this Angular 6 tutorial, you'll learn by building a real world example application:
You can use the following commands to start the development server:
# Create and migrate the database then run the local development server
1 / 15
techiediaries.com
We are using pipenv, the officially recommended package management tool for Python so you'll need to have
it installed. The process is quite simple depending on your operating system.
You can check the installed version by running the following command:
ng version
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Package Version
------------------------------------------------------
@angular-devkit/architect 0.6.0
@angular-devkit/core 0.6.0
@angular-devkit/schematics 0.6.0
@schematics/angular 0.6.0
@schematics/update 0.6.0
rxjs 6.1.0
typescript 2.7.2
2 / 15
techiediaries.com
Now, you're ready to create a project using Angular CLI v6. Simply run the following command in your
terminal:
ng new crmapp
The CLI will automatically generate a bunch of files common to most Angular 6 projects and install the
required dependencies for your project.
We will mostly be working inside the src/app folder. This is the directory structure of the project:
You can serve your application locally by running the following commands:
A component is a TypeScript class with an HTML template and an optional set of CSS styles that control a
part of the screen.
Components are the most important concept in Angular 6. An Angular 6 application is basically a tree of
components with a root component (the famous AppComponent). The root component is the one contained in
the bootstrap array in the main NgModule module app.module.ts.
One important aspect of components is re-usability. A component can be re-used throughout the application
and even in other applications. Common and repeatable code that performs a certain task can be
encapsulated into a re-usable component that can be called whenever we need the functionality it provides.
Each bootstrapped component is the base of its own tree of components. Inserting a bootstrapped
component usually triggers a cascade of component creations that fill out that tree. source
Component-Based Architecture
An Angular application is made of several components forming a tree structure with parent and child
components.
A component is an independent block of a big system (web application) that communicates with the other
building blocks (components) of the system using inputs and outputs. A component has associated view, data
and behavior and may have parent and child components.
Components allow maximum re-usability, easy testing, maintenance and separation of concerns.
Let's now see this practically. Head over to your Angular application project folder and open the src/app
folder. You will find the following files:
4 / 15
techiediaries.com
Except for the last file which contains the declaration of the application main (root) Module, all these files are
used to create a component. It's the AppComponent: The root component of our application. All other
components we are going to create next will be direct or un-direct children of the root component.
Go ahead and open the src/app/app.component.ts file and let's understand the code behind the
main/root component of the application.
We first import the Component decorator from @angular/core then we use it to decorate the TypeScript
class AppComponent. The Component decorator takes an object with many parameters such as:
selector: specifies the tag that can be used to call this component in HTML templates just like the
standard HTML tags
templateUrl: indicates the path of the HTML template that will be used to display this component (you
can also use the template parameter to include the template inline as a string)
styleUrls: specifies an array of URLs for CSS style-sheets for the component
The export keyword is used to export the component so that it can be imported from other components and
modules in the application.
The title variable is a member variable that holds the string 'app'. There is nothing special about this variable
and it's not a part of the canonical definition of an Angular component.
Now let's see the corresponding template for this component. If you open src/app/app.component.html
this is what you'll find:
<div style="text-align:center">
<h1>
Welcome to {{ title }}!
</h1>
<img width="300" alt="Angular Logo"
src="data:image/svg+xml;....">
</div>
5 / 15
techiediaries.com
<li>
<h2><a target="_blank" rel="noopener"
href="https://angular.io/tutorial">Tour of Heroes</a></h2>
</li>
<li>
<h2><a target="_blank" rel="noopener"
href="https://github.com/angular/angular-cli/wiki">CLI Documentation</a>
</h2>
</li>
<li>
<h2><a target="_blank" rel="noopener"
href="https://blog.angular.io/">Angular blog</a></h2>
</li>
</ul>
The template is a normal HTML file (almost all HTML tags are valid to be used inside Angular templates
except for some tags such as <script>, <html> and <body> etc.) with the exception that it can contain
template variables (in this case the title variable) or expressions ({{...}}) that can be used to insert values
in the DOM dynamically. This is called interpolation or data binding. You can find more information about
templates from the docs.
You can also use other components directly inside Angular templates (via the selector property) just like
normal HTML.
If you are familiar with the MVC (Model View Controller) pattern, the component class plays the role of the
Controller and the HTML template plays the role of the View.
Before adding routing to our application we first need to create the application's components so based on the
exposed REST API architecture we can initially divide our application into these components:
6 / 15
techiediaries.com
AccountCreateComponent: this component displays and controls a form for creating or updating
accounts
You can see that the command generates all the files to define a component and also updates
src/app/app.module.ts.
If you open src/app/app.module.ts after running all commands, you can see that all components are
automatically added to the AppModule declarations array.:
7 / 15
techiediaries.com
@NgModule({
declarations: [
AppComponent,
AccountListComponent,
AccountCreateComponent,
ContactListComponent,
ContactCreateComponent,
LeadListComponent,
LeadCreateComponent,
OpportunityListComponent,
OpportunityCreateComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
If you are creating components manually, you need to make sure to include manually so they can be
recognized as part of the module.
8 / 15
techiediaries.com
Angular CLI provides the --routing switch (ng new crmapp --routing) that enables you to add routing
automatically but we're going to add routing manually for the sake of understanding the various pieces
involved in adding component routing to your Angular application.
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
The routes will contain all the routes of the application. After creating the components we'll see how to add
routes to this array.
For now, we want to redirect the visitor to the /accounts path when the home URL is visited so the first path
we'll add is:
The pathMatch specifies the matching strategy. full means that we want to fully match the path.
9 / 15
techiediaries.com
{
path: 'contacts',
component: ContactListComponent
},
{
path: 'create-contact',
component: ContactCreateComponent
},
{
path: 'leads',
component: LeadListComponent
},
{
path: 'create-lead',
component: LeadCreateComponent
},
{
path: 'opportunities',
component: OpportunityListComponent
},
{
path: 'create-opportunity',
component: OpportunityCreateComponent
}
];
Now open src/app/app.module.ts and import the routing module then add it to the imports array:
@NgModule({
declarations: [
AppComponent,
[...]
],
imports: [
BrowserModule,
AppRoutingModule
],
[...]
})
export class AppModule { }
Finally, open src/app/app.component.html then add the navigation links and the router outlet:
10 / 15
techiediaries.com
<div>
<router-outlet></router-outlet>
</div>
First, you need to add the HttpClientModule module to the imports array of the main application module
[..]
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
..
],
imports: [
[..]
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
A service is a global class that can be injected in any component. It's used to encapsulate code that can be
common between multiple components in one place instead of repeating it throughout various components.
11 / 15
techiediaries.com
Now, lets create a service that encapsulates all the code needed for interacting with the REST API. Using
Angulat CLI run the following command:
ng g service api
@Injectable({
providedIn: 'root'
})
Angular 6 provides a way to register services/providers directly in the @Injectable() decorator by using
the new providedIn attribute. This attribute accepts any module of your application or 'root' for the main
app module. Now you don't have to include your service in the providers array of your module.
First we'll add a method to consume this endpoint in our global API service,
next we'll inject the API service and call the method from the corresponding component class
(ContactListComponent)
and finally we'll display the result (the list of contacts) in the component template.
12 / 15
techiediaries.com
@Component({
selector: 'app-contact-list',
templateUrl: './contact-list.component.html',
styleUrls: ['./contact-list.component.css']
})
<h1>
My Contacts
</h1>
<div>
<table style="width:100%">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone</th>
<th>Email</th>
<th>Address</th>
</tr>
<tr *ngFor="let contact of contacts">
<td> { { contact.first_name } } </td>
<td> { { contact.last_name } } </td>
<td> { { contact.phone } } </td>
<td> { { contact.email } } </td>
<td> { { contact.address } } </td>
</tr>
</table>
13 / 15
techiediaries.com
</div>
Now let's create a method to send HTTP Post request to create a random contact. Open the API service file
and add the following method:
createContact(contact){
return this.httpClient.post(`${this.API_URL}/contacts/`,contact);
}
Next let's call this method from the ContactCreateComponent to create a contact. First open
src/app/contact-create/contact-create.component.ts and add the following code:
@Component({
selector: 'app-contact-create',
templateUrl: './contact-create.component.html',
styleUrls: ['./contact-create.component.css']
})
ngOnInit() {}
createContact(){
var contact = {
account: 1,
address: "Home N 333 Apartment 300",
createdBy: 1,
14 / 15
techiediaries.com
For now, we're simply hard-coding the contact info for the sake of simplicity.
<h1>
Create Contact
</h1>
<button (click)="createContact()">
Create Contact
</button>
Conclusion
Throught this Angular 6 tutorial, we've seen , by building a simple real world example, how to use different
Angular concepts to create simple full-stack application with Angular and Django. You can find the source
code in this repository.
15 / 15