Author: saqibkhan

  • Implement Components

    Home Component

    // src/app/home/home.component.ts
    import { Component } from '@angular/core';
    
    @Component({
      selector: 'app-home',
      template: <h1>Welcome to the Home Page</h1>
    })
    export class HomeComponent { }
    

    About Component

    // src/app/about/about.component.ts
    import { Component } from '@angular/core';
    
    @Component({
      selector: 'app-about',
      template: <h1>About Us</h1><p>This is the about page.</p>
    })
    export class AboutComponent { }
    

    User Component

    // src/app/user/user.component.ts
    import { Component, OnInit } from '@angular/core';
    import { UserService } from '../user.service';
    
    @Component({
      selector: 'app-user',
      template: `
    
    <h1>User List</h1>
    <ul>
      <li *ngFor="let user of users">
        {{ user.name }} ({{ user.email }})
      </li>
    </ul>
    ` }) export class UserComponent implements OnInit { users: any[] = []; constructor(private userService: UserService) { } ngOnInit(): void {
    this.userService.getUsers().subscribe(data => {
      this.users = data;
    });
    } }
  • Add HttpClientModule

    Import HttpClientModule in your AppModule:

    // src/app/app.module.ts
    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { HttpClientModule } from '@angular/common/http';
    
    import { AppRoutingModule } from './app-routing.module';
    import { AppComponent } from './app.component';
    import { HomeComponent } from './home/home.component';
    import { AboutComponent } from './about/about.component';
    import { UserComponent } from './user/user.component';
    
    @NgModule({
      declarations: [
    
    AppComponent,
    HomeComponent,
    AboutComponent,
    UserComponent
    ], imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule
    ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
  • Implement a Service

    Implement a basic UserService to fetch user data:

    // src/app/user.service.ts
    import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { Observable } from 'rxjs';
    
    @Injectable({
      providedIn: 'root'
    })
    export class UserService {
      private apiUrl = 'https://jsonplaceholder.typicode.com/users';
    
      constructor(private http: HttpClient) { }
    
      getUsers(): Observable<any[]> {
    
    return this.http.get&lt;any&#91;]>(this.apiUrl);
    } }
  • Define Routing

    Modify src/app/app-routing.module.ts to set up routing for your application:

    import { NgModule } from '@angular/core';
    import { RouterModule, Routes } from '@angular/router';
    import { HomeComponent } from './home/home.component';
    import { AboutComponent } from './about/about.component';
    import { UserComponent } from './user/user.component';
    
    const routes: Routes = [
      { path: '', component: HomeComponent },
      { path: 'about', component: AboutComponent },
      { path: 'user', component: UserComponent }
    ];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }
    
  • Generate Components and Service

    Generate a few components and a service for demonstration:

    ng generate component home
    ng generate component about
    ng generate component user
    ng generate service user
    
  • Setup Your Angular Application

    First, create a new Angular application using the Angular CLI:

    ng new angular-example
    cd angular-example
    ng serve
    
  • Master Angular CLI

    The Angular Command Line Interface (CLI) is a powerful tool that can streamline your development process. Learn to use its various commands to generate components, services, modules, and more. For example:

    • ng generate component component-name or ng g c component-name for generating components.
    • ng serve to start your development server.
    • ng build --prod for production builds, which includes optimization.
  • Real-World Example

    Building a Sample Angular Application

    • Overview of the application features.
    • Step-by-step guide to building the app.
    • Integrating with a backend service.
  • Migration and Upgrading

    Upgrading from Angular 9 to Angular 10+

    • Breaking changes and new features.
    • Using the Angular Update Guide.

    Best Practices for Maintaining an Angular Application

    • Keeping dependencies up-to-date.
    • Following Angular style guides and best practices.
  • Angular CLI

    Common CLI Commands

    • Generating components, services, modules.
    • Building and serving the application.

    Customizing the Angular CLI Configuration

    • Modifying angular.json.
    • Adding custom scripts and configurations.