Thursday, October 29, 2020

NgFor Directive in Angular

 *ngFor Directive in Angular

NgFor in Angular is a built-in template directive that makes it easy to iterate over something like an array or an object and create a template for each item.

NgFor Directive in Angular

Here’s a basic example of its use:

<ul>

  <li *ngFor="let user of users">{{ user.name }}</li>

</ul>

This will output html that looks like this:

<ul>

  <li>Al Dente</li>

  <li>D. Liver</li>

  <li>Pepe Roni</li>

</ul>

let user creates a local variable that will be available in the template.

of users means that we’ll be iterating over the users iterable that should be made available in our component.

The * character before ngFor creates a parent template. It’s a shortcut to the following syntax: template=“ngFor let item of items”.

Available local variables

You can also set local variables for the following exported values: index, first, last, even and odd. index will return the current loop index, and the other values with provide a boolean indicating if the value is true or false. For example:

<ul>

  <li *ngFor="let user of users; let i = index; let odd = odd"

      [class.odd]="odd">

    {{i + 1}}. {{ user.name }}

  </li>

</ul>

Will produce the following markup:

<ul>

  <li>1. Al Dente</li>

  <li class="odd">2. D. Liver</li>

  <li>3. Pepe Roni</li>

</ul>

See Also

  • *ngIf
  • *ngSwitch

Saturday, October 24, 2020

Lazy Loaded Module Example in Angular 10/9 with loadChildren

Lazy loading is the process of loading some features of your Angular application only when you navigate to their routes for the first time. This can be useful for increasing your app performance and decreasing the initial size of the bundle transmitted to the user's browser.

In Angular 10 and previous versions till Angular 8, the syntax for lazy-loading modules has changed and it's now more aligned with the standard browser's API.

You now need to use the dynamic import syntax to import your module in the loadChildren property of Angular Router routes.

Lazy Loaded Module Example in Angular 10/9 with loadChildren

The dynamic import API is a standard browser's API introduced in modern browers. It's promise-based and gives you access to the module, from where the module's class can be called.

Setting up an Angular 10 Project

You need to have Angular CLI 10 installed and an Angular 10 project with routing setup.

Adding an Angular 10 Module

We can only lazy-load modules in Angular so let's generate a feature module using the Angular CLI 10:

$ ng generate module admin

Next, we can also add a couple of components in our module:

$ ng generate component admin/login

$ ng generate component admin/dashboard

Using loadChildren to Lazy-Load your Angular 10 Module

Angular provides the loadChildren property of a route's path to specify the module that needs to be lazy loaded when it's first navigated to.

Open the src/app/app-routing.module.ts file and update it as follows:

import { NgModule } from '@angular/core';

import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [

{ path: 'admin', loadChildren: () => import(`./admin/admin.module`).then(m => m.AdminModule) },

];

Configuring Routes in your Angular 10 Feature Module

After configuring the route to lazy-load your feature module, you'll next need to add routing to the various components of the admin module which needs to have its own routes seprated from the main routing module that resides in the src/app/app-routing.module.ts file.

Go ahead and create a admin/admin-routing.module.ts file and add the following code:

import { Routes } from '@angular/router';

import { RouterModule } from  '@angular/router';

import { LoginComponent } from './login/login.component';

import { DashboardComponent } from './dashboard/dashboard.component';

const routes: Routes = [

    { path: '', component: LoginComponent },

    { path: 'dashboard', component: dashboardComponent }

];

@NgModule({

  imports: [RouterModule.forChild(routes)],

  exports: [RouterModule]

})

export class AdminRoutingModule { }

We create the routes to the various components of our admin module and we include these routes in the admin routing module.

We need to feed the routes to the Angular Router using the forChild() method instead of the forRoot() module.

Open the src/app/admin/admin.module.ts file and import the exported admin routing module as follows:

import { AdminRoutingModule } from './admin-routing.module';

Next, add it to the imports array of the admin module:

@NgModule({

  imports: [

    CommonModule,

    AdminRoutingModule

  ],

  declarations: [LoginComponent, DashboardComponent]

})

export class AdminModule { }

Angular 9/8 Nested Routing and Child Routes Example

You can create a nested routing by defining child routes using the children property of a route (alongside a path and component properties). You also need to add a nested router-outlet in the HTML template related to the component linked to the parent route (In our case it's the admin route).

To create nested routing, you need to create a routing submodule for the module you want to provide routing, you next need to define a parent route and its child routes and provide them to the router configuration via a forChild() method.

Angular 9/8 Nested Routing and Child Routes Example

Creating an Angular 9 Routing Module

Let's see this step by step. First, inside the admin module, create an admin-routing.module.ts file and add a submodule for implementing child routing in our admin module:

import { NgModule } from  '@angular/core';

import { Routes, RouterModule } from  '@angular/router';

import { ProjectComponent } from  './project/project.component';

import { ProjectListComponent } from  './project-list/project-list.component';

import { ProjectCreateComponent } from  './project-create/project-create.component';

import { ProjectUpdateComponent } from  './project-update/project-update.component';

const  routes:  Routes  = [

{

path:  'admin',

component:  ProjectComponent,

children: [

{

path:  'list',

component:  ProjectListComponent

},

{

path:  'create',

component:  ProjectCreateComponent

},

{

path:  'update',

component:  ProjectUpdateComponent

}

]

}

];

@NgModule({

imports: [RouterModule.forChild(routes)],

exports: [RouterModule]

})

export  class  AdminRoutingModule { }

This is an example of a module which has imports and exports meta information;

The imports array which contains the modules that we need to import and use in the current module. In this case it's RouterModule.forChild(routes),

The exports array which contains what we need to export.

Adding Child Routes to the Angular 9 Router Module

In order to provide our child routes to the router module, we use the forChild() method of the module because we want to add routing in the admin submodule. if this is used in root module you need to use the forRoot() method instead. See more differences of forChild() vs forRoot() from the official docs.

The forChild() and forRoot() methods are static methods that are used to configure modules in Angular. They are not specific to RouterModule.

We are creating a parent admin route and its own child routes using the children property of the route which takes an array of routes.

You can respectively access the ProjectListComponent, ProjectCreateComponent and ProjectCreateComponent using the /admin/list, /admin/create and /admin/update paths.

Next, open the src/app/admin/admin.module ts file and import the routing module:

// [..]

import { AdminRoutingModule } from  './admin-routing.module';

@NgModule({

// [...]

imports: [

    CommonModule,

    AdminRoutingModule

]

})

export  class  AdminModule { }

Next open the src/app/admin/project/project.component html file and add a nested router outlet:

<h2>Admin Interface</h2>

<router-outlet></router-outlet>

This is a nested router-outlet that will be only used to render the components of the admin module i.e ProjectListComponent, ProjectCreateComponent and ProjectCreateComponent.

Note: If you don't add a nested router outlet in the parent route, child components will be rendered in the parent router outlet of the application.

Next in the src/app/header/header.component.html file, add a link to access the admin interface:

<li  class="nav-item">

<a  class="nav-link"  routerLink="/admin/list">Admin</a>

</li>

At this point, if you click on the admin link in the header, you should see the following interface:

Sunday, October 18, 2020

The beauty of Hanoi through the wool of a foreigner in Vietnam

The most well known and important districts in Hanoi are Ba Dinh District (aka the French Quarter) where the government offices are located and Hoan Kiem District (aka the Old Quarter) which is considered the city’s business hub and main tourist destination.

One of Hanoi’s most common sights is that of streets packed with scooters, bicycles and cars swarming around pedestrians like a school of fish.

Hanoi Walking Streets – Hoan Kiem Lake

Hanoi Walking Streets – Hoan Kiem Lake
Along with famous destinations like Ngoc Son Temple, Hoa Lo prison historical relic site, and Bach Ma Temple, the pedestrian zone around Hoan Kiem Lake in downtown Hanoi has become an attraction that lure throngs of visitors to the capital on the weekends. At present, the Hanoi pedestrian street around Hoan Kiem Lake has been gradually becoming one of the capital’s tourist attractions, a place for people to go out, play, and slow down between the roads.

The pedestrian is so crowded especially during the weekends, and roads surrounding the area are blocked with barricades so that the pedestrians can freely walk around on the streets on Saturday and Sunday.

St. Joseph’s Cathedral (Vietnamese:

St. Joseph’s Cathedral (Vietnamese
Construction began in 1886, with the architectural style described as resembling Notre Dame de Paris. The church was one of the first structures built by the French colonial government in Indochina when it opened in December 1886. It is the oldest church in Hanoi.

The cathedral conducts mass several times during the day. For Sunday evening mass at 6:00 PM, large crowds spill out into the streets. The prayer hymns are broadcast and Catholics who are unable to enter the cathedral congregate in the street and listen to hymns.
The Old Quarter is the name commonly given to the historical civic urban core of Hanoi, located outside the Imperial Citadel of Thăng Long. This quarter used to be the residential, manufacturing and commercial center, where each street was specialized in one specific type of manufacturing or commerce.
Another common name referring to approximately the same area is the 36 streets (Vietnamese: Hà Nội 36 phố phường), after the 36 streets or guilds that used to make up the urban area of the city.

Hanoi’s Long Biên Bridge

Hanoi’s Long Biên Bridge