Angular

Components


In this lesson we will build a very basic component.

Table of Contents


Angular Architecture

Angular

Angular application has at least one Angular module called application root module. Every module has a set of components that are the building blocks of the application, and some services, that provide functionality across those components (common data and logic). Angular module organizes all together.

For each application we create module/s with components and arange them to form our application.

angular architecture
angular architecture

In this lecture we will concentrate on building a single module - application root module (AppModule). It has at least one component called application root component (AppComponent). AppComponent is the starting point of the application. AppModule will use some Angular predefined modules.

AppComponent

Bootstrapping the Application

When we generate a new project, Angular generates the application initial files.


Generate a new project (by the ng new command) and look at the project files in the src folder. Files outside src/ concern building, deploying, and testing your application. They include configuration files and external dependencies. Files inside src/ "belong" to your application.
files
bootstrapping the application

AppComponent is the starting point of the application:
The browser shows the HTML code defined by the app-root selector in the AppComponent file (application root component), and not the original code defined in the index.html app-root tag. Angular is a JS framework, changing the DOM (HTML) at run-time.

AppComponent

Change app.component.ts file content to the following:
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: '<h1>My First Component</h1>',
  styles: ['h1 { color: navy; font-style: italic; }']
})
export class AppComponent {
}
Activate ng serve and check the display.

Components

Components are the building blocks of your website or application. Every component consists of three sections:

component

Analyze the content of the app.component.ts file in the previous assignment.

You can choose to either create a component manually, or use the angular-cli to generate one for you.

Note: in this lecture we define the component selector as a new tag name which has to be unique. Selector can be defined in a few other ways. Selectors are like CSS selectors. They can be attribute selectors, tag selectors, class selectors, id selectors and combinations of these. We will use tag selectors only in this lecture.


Templating

You define a component's view with its companion template. A template is a form of HTML that tells Angular how to render the component. A template looks like regular HTML with some Angular extensions (will see it later).

All components must define a selector, a template and styles in the @Component decorator. There are two ways in which you can specify both HTML and CSS.

If your component only requires minimal HTML or CSS, it's best to use inline within the component decorator as it increases speed because it doesn't require loading an external file.

If your component consists of a lot of HTML or CSS, it is better to write it in separate files.

template

External HTML and/or CSS

It’s common practice to split a component’s code, HTML, and CSS into three separate files in the same directory. This is cleaner for a big code and also in a team with designers and several developers editing the same code. But this will result in an extra HTTP request per template. For external files, you have to use the metadata property templateUrl for HTML and styleUrls for CSS to define the location of an external HTML and CSS files. You can use a relative URL by prefixing your filenames with ./ like the following:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']      // an array of stylesheet paths
})

Inline HTML and/orCSS

You can choose to write the HTML and CSS as inline strings (in single or double quotes) directly, within the @Component decorator data with the property template for HTML, and styles for CSS. It has the advantage that at runtime there is no extra HTTP request, but this method might not scale well for large templates/css. It also misses syntax checkings.

@Component({
  selector: 'app-root',
  template: '<p>My HTML Here</p>',
  styles: ['p { font-size: 1.2em; }']      // an array of style strings
})

If you wish to write multi-line HTML and/or CSS, you must change the single quotes ('') to backticks (``):

@Component({
  selector: 'app-root',
  template: `
      <p>My HTML Here</p>
      <div>More HTML here</p>
  `,
  styles: [`
      p { font-size: 1.2em; }
      body { color: #000; }
  `]
})

Final Assignment

The following summarizes the materials in this lesson.

Generate a new project. The final page looks like:

header component

Instructions:

Generate my-header component. my-header component has to include the header with styling. Add my-header to the app component template. Note that in such a way we create nested component (my-header component inside app component).

Define all styles which are common to different application views in the src/styles.css file. Define header styles in the my-header component css.

Solution:

Look the result here.

You can find the solution files here. It includes all the src files. To run the solution, you have to generate a new project and append the solution src files to the project.