-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Server-Side Enterprise Development with Angular
By :
One of the great things about working with Angular is that it promotes building applications in a modular and componentized way. In Angular, NgModule (or simply Module) is a way to group an application into logical blocks of functionality. A Module is a TypeScript class with the @NgModule decorator. In the decorator, we define how Angular compiles and runs the code inside the module.
In this chapter, we are going to build a module that groups together the components we want to use in the application's global user interface. We will create a LayoutComponent that consists of a HeaderComponent and a FooterComponent, and in between those we will define the space where the actual application logic will be displayed:
In this section, we will generate the UiModule using the ng command, import the UiModule in the AppModule, and add Router Outlet to the AppComponent.
Using the ng generate command, we can generate or scaffold out all sorts of code that can be used in an Angular application. In this exercise, we will use the ng generate module command to generate the UiModule. This command has one required parameter, which is the name. In this case, we use ui. Follow these steps to complete this exercise:
ng generate module ui CREATE src/app/ui/ui.module.ts (186 bytes)
As you can see by the output of the preceding command, the UiModule is generated in the new folder called src/app/ui.
When we take a look at this file, we can see what an empty Angular module looks like:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
imports: [
CommonModule
],
declarations: []
})
export class UiModule { }
Now that the UiModule has been created, we need to import it from the AppModule. This way, we can use the code inside the UiModule from other code that lives inside the AppModule. Follow these steps to complete this exercise:
src/app/app.module.ts file.import statement at the top of the file:import { UiModule } from './ui/ui.module';UiModule in the imports array inside the NgModule decorator:@NgModule({
...
imports: [
// other imports
UiModule
],
...
})The UiModule has now been created and imported in the AppModule, which makes it ready to use:
Let's go ahead and create the first component inside the UiModule to make it display in the app!
When building an Angular app, you generally lean on Angular's router to tie all of the modules and components together. We will build all the application logic in modules and use the AppComponent to display the current route.
For this to work, we need to update the AppComponent template and define the router-outlet component. Follow these steps to complete this exercise:
src/app/app.component.html file.<router-outlet></router-outlet>
After refreshing the app, we should see a blank page. This is because we don't have any routes set up, and thus there is no way that the Angular app knows what to display.
Let's move to the next topic so that we can create the basic layout.
In this section, you will use ng generate to create the LayoutComponent inside the UiModule, add the LayoutComponent to the AppRoutingModule so that it gets displayed, and implement the template of the LayoutComponent.
The LayoutComponent is the main template of the application. The function of this component is to glue together the HeaderComponent and the FooterComponent and show the actual application pages in between those two.
In this exercise, we will use the ng generate command to create the LayoutComponent. Follow these steps to complete this exercise:
ng generate component ui/components/layout CREATE src/app/ui/components/layout/layout.component.css (0 bytes) CREATE src/app/ui/components/layout/layout.component.html (25 bytes) CREATE src/app/ui/components/layout/layout.component.spec.ts (628 bytes) CREATE src/app/ui/components/layout/layout.component.ts (269 bytes) UPDATE src/app/ui/ui.module.ts (273 bytes)
We can see that the component was created in the new src/app/ui/components directory:
The last line of the output shows us that the UiModule got updated.
When we open the UiModule in the editor, we can see that it added an import for the LayoutModule and added it to the declarations array in the NgModule decorator.
Using declarations, we declare the existence of components in a module so that Angular knows that they exist and can be used:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LayoutComponent } from './components/layout/layout.component';
@NgModule({
imports: [
CommonModule
],
declarations: [LayoutComponent]
})
export class UiModule { }
As described in the introduction of this section, we will use the LayoutComponent as the base for the whole application. It will display the header, footer, and a router outlet to show the actual application screens. We will leverage Angular's built-in routing mechanism to do this. We will add a new route to the routing array and reference the LayoutComponent in this route's component.
Follow these steps to complete this exercise:
src/app/app-routing.module.ts file.import statement to the list of imports at the top of the file:import { LayoutComponent } from './ui/components/layout/layout.component';routes property, we will add a new object literal.path property and set its value to an empty string, ''.component property and set its value to reference the LayoutComponent that we just imported.The line of code that we must add to the routes array is as follows:
{
path: '',
component: LayoutComponent,
children: [] ,
}For reference, the complete file should look like this:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LayoutComponent } from './ui/components/layout/layout.component';
const routes: Routes = [
{
path: '',
component: LayoutComponent,
children: [],
}
] ;
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
When the application refreshes, we should see the text layout works!:
In this exercise, we'll get rid of this default text and start implementing the template. Follow these steps to complete this exercise:
src/app/ui/layout/layout.component.html file.<h1>header placeholder</h1> <div class="container my-5"> <router-outlet></router-outlet> </div> <h1>footer placeholder</h1>
When we save the file, we will see that the browser outputs a blank page.
Looking in the Console tab from Chrome Developer Tools, we can see that we have an error stating Template parse errors: 'router-outlet' is not a known element:.

To make the router-outlet available to be used in the LayoutComponent, we need to import the RouterModule in UiModule.
src/app/ui/ui.module.ts.import statement to the list of imports at the top of the file:import { RouterModule } from '@angular/router';RouterModule inside the imports array in the NgModule decorator.When we now save the file, we should see the placeholders for the header and footer, with some whitespace in-between and the router error gone from the console:
Now that that's done, let's add some content to the placeholders.
In this section, you will download a logo to use in the application header, implement the header with a dynamic title and navigation items, and implement the footer with dynamic text.
We will now download the Angular logo and place it in the assets folder:
https://angular.io/assets/images/logos/angular/angular.svg file.src/assets/logo.svg in the project directory.In this exercise, we will add the header to LayoutComponent. We will define three class properties: a string for the application logo, a title, and an array of objects that represent the navigation items we want to display in the header.
In the template, we will create a Bootstrap navbar consisting of a nav element with some styles, a link with the logo, the title, and the navigation items. Follow these steps to complete this exercise:
src/app/ui/components/layout/layout.component.ts file.public logo = 'assets/logo.svg';
public title = 'Angular Social';
public items = [{ label: 'Posts', url: '/posts'}];
src/app/ui/components/layout/layout.component.html file.<nav class="navbar navbar-expand navbar-dark bg-dark">
<a class="navbar-brand" routerLink="/">
<img [src]="logo" width="30" height="30" alt="">
{{title}}
</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav">
<li class="nav-item" *ngFor="let item of items" routerLinkActive="active">
<a class="nav-link" [routerLink]="item.url">
{{item.label}}
</a>
</li>
</ul>
</div>
</nav>
When we save this file and check in the browser, we will finally see the first part of the application being displayed:
In this exercise, we will add the footer to the LayoutComponent.
We will define two class properties, a string property for the name of the developer and the year.
In the template, we will create another Bootstrap navbar consisting of a nav element with some styles and the copyright message that uses both string properties we defined in our component class. Follow these steps to complete this exercise:
src/app/ui/components/layout/layout.component.ts file.public developer = 'YOUR_NAME_PLACEHOLDER'; public year = 'YEAR_PLACEHOLDER';

src/app/ui/components/layout/layout.component.html file.<nav class="navbar fixed-bottom navbar-expand navbar-dark bg-dark">
<div class="navbar-text m-auto">
{{developer}} <i class="fa fa-copyright"></i> {{year}}
</div>
</nav>
When we save this file and check it in the browser, we will see that both the header and footer are being displayed:
We are done with the layout. Let's start building the actual application logic.
We can refactor the UiModule by creating separate components for the header and the footer. The following activities should be completed using the knowledge that you have learned in this section.
In this activity, you will create a HeaderComponent in src/app/ui/components/. Reference the HeaderComponent in the LayoutComponent so that it says header works!.
After you have done this, you can copy the header markup and class properties from the LayoutComponent to the HeaderComponent.
The steps are as follows:
HeaderComponent.HeaderComponent from the LayoutComponent.layout.component.html to header.component.html.layout.component.ts to header.component.ts.The solution for this activity can be found on page 110.
In this activity, we will create a FooterComponent, similar to and based on the instructions from the previous activity.
The steps are as follows:
FooterComponent.FooterComponent from the LayoutComponent.layout.component.html to footer.component.html.layout.component.ts to footer.component.ts.The solution for this activity can be found on page 111.
Change the font size
Change margin width
Change background colour