Welcome to this comprehensive Angular tutorial, where we will guide you through the process of building web applications using the Angular framework. Angular is a powerful JavaScript framework developed by Google, designed to simplify the development of dynamic, single-page applications. Whether you are a beginner or an experienced developer, this tutorial will provide you with a solid foundation in Angular and its core concepts.
npm install -g @angular/cli
Open your terminal or command prompt and navigate to the desired directory where you want to create your Angular project.
Run the following command to create a new Angular project:
ng new my-angular-app
After the project is created, navigate into the project directory:
cd my-angular-app
ng serve
Open your web browser and navigate to http://localhost:4200. You should see the default Angular application running.
ng generate component my-component
Open the src/app directory and you will find a new directory named my-component. Inside this directory, you'll see the generated files for the component.
<h1>Welcome to My Angular App</h1>
<app-my-component></app-my-component>
Open src/app/my-component/my-component.component.ts and replace the generated code with the following:
import { Component } from '@angular/core';
@Component({
selector: 'app-my-component',
template: `
<p>This is my custom component!</p>
`,
})
export class MyComponentComponent {}
ng generate service my-service
Open the src/app directory and you will find a new file named my-service.service.ts. Open it and replace the generated code with the following:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class MyServiceService {
getData(): string {
return 'This is the data from the service.';
}
}
+
import { Component } from '@angular/core';
import { MyServiceService } from '../my-service.service';
@Component({
selector: 'app-my-component',
template: `
{{ data }}
`,
})
export class MyComponentComponent {
data: string;
constructor(private myService: MyServiceService) {}
ngOnInit() {
this.data = this.myService.getData();
}
}
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { MyComponentComponent } from './my-component/my-component.component';
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: MyComponentComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Open src/app/app.component.html and modify the code as follows:
<h1>Welcome to My Angular App</h1>
<nav>
<a routerLink="/home" routerLinkActive="active">Home
</nav>
<router-outlet></router-outlet>
<form (ngSubmit)="onSubmit()">
<label for="name">Name:</label>
<input type="text" id="name" [(ngModel)]="name" name="name">
<button type="submit">Submit</button>
</form>
Open src/app/my-component/my-component.component.ts and modify the code as follows:
import { Component } from '@angular/core';
@Component({
selector: 'app-my-component',
template: `
<form (ngSubmit)="onSubmit()">
<label for="name">Name:</label>
<input type="text" id="name" [(ngModel)]="name" name="name">
<button type="submit">Submit</button>
</form>
`,
})
export class MyComponentComponent {
name: string;
onSubmit() {
console.log('Submitted: ' + this.name);
}
}
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class MyServiceService {
constructor(private http: HttpClient) {}
getDataFromAPI() {
return this.http.get('https://api.example.com/data');
}
}
Open src/app/my-component/my-component.component.ts and modify the code as follows:
import { Component } from '@angular/core';
import { MyServiceService } from '../my-service.service';
@Component({
selector: 'app-my-component',
template: `
{{ data }}
`,
})
export class MyComponentComponent {
data: string;
constructor(private myService: MyServiceService) {}
getData() {
this.myService.getDataFromAPI().subscribe(
response => {
this.data = response;
},
error => {
console.log('Error occurred:', error);
}
);
}
}
Congratulation
The objective of this website is to Train the people in software field, develop them as good human resources and deploy them in meaningful employment. The high level of hands-on-training and case studies ensures that Trainee gain the optimum benefits. Online Tutorial on Technology subjects as below with Interview Questions and Answers.