A component controls a patch of screen called a view.
Component's application logic is defined inside a class. The class interacts with the view through an API of properties and methods
export class BookDetailsComponent implements OnInit {
currentBook = new Book();
ngOnInit(): void {
this.currentBook.author = 'John Example';
this.currentBook.title = 'Angular in a nutshell';
}
}
export class BookDetailsComponent implements OnInit {
currentBook = new Book();
ngOnInit(): void {
this.currentBook.author = 'John Example';
this.currentBook.title = 'Angular in a nutshell';
}
}
export class BookDetailsComponent implements OnInit {
currentBook = new Book();
ngOnInit(): void {
this.currentBook.author = 'John Example';
this.currentBook.title = 'Angular in a nutshell';
}
}
Angular creates, updates, and destroys components as the user moves through the application. Your app can take action at each moment in this lifecycle through optional lifecycle hooks
Examples for lifecycle hooks:
Constructor, NgOnInit & NgOnDestroy
<div>
<label>Author:</label> <span>{{currentBook.author}}</span>
</div>
<div>
<label>Title:</label> <span>{{currentBook.title}}</span>
</div>
<div>
<label>Author:</label> <span>{{currentBook.author}}</span>
</div>
<div>
<label>Title:</label> <span>{{currentBook.title}}</span>
</div>
A template is a form of HTML that tells Angular how to render the component.
A template looks like regular HTML with Angularized markup which uses Angular's template syntax.
Component Metadata tells Angular where to get the major building blocks you specify for the component.
@Component({
selector: 'app-book-details',
template:
`<div>
<label>Author:</label> <span>{{currentBook.author}}</span>
</div>
<div>
<label>Title:</label> <span>{{currentBook.title}}</span>
</div>`
})
export class BookDetailsComponent implements OnInit {
...
}
selector tells Angular to create and insert an instance of this component where it finds a <app-book-details> tag in parent HTML
@Component({
selector: 'app-book-details',
template:
`<div>
<label>Author:</label> <span>{{currentBook.author}}</span>
</div>
<div>
<label>Title:</label> <span>{{currentBook.title}}</span>
</div>`
})
export class BookDetailsComponent implements OnInit {
...
}
Templates can be inlined, e.g. using ES2015 template literals (multi line strings marked with backticks) or ...
@Component({
selector: 'app-book-details',
template:
`<div>
<label>Author:</label> <span>{{currentBook.author}}</span>
</div>
<div>
<label>Title:</label> <span>{{currentBook.title}}</span>
</div>`
})
export class BookDetailsComponent implements OnInit {
...
}
...extracted to a separate file and imported using file path
@Component({
selector: 'app-book-details',
templateUrl: './book-details.component.html'
})
export class BookDetailsComponent implements OnInit {
...
}
Components are styled using a separate css file that is only applied to the component and not to subordinate components.
@Component({
selector: 'app-book-details',
template: ...,
styleUrls: ['./book-details.component.css']
})
export class BookDetailsComponent implements OnInit {
...
}
In TypeScript metadata can be attached to the component class by using a decorator.
Angular supports two ways of making a component available to other components:
as a standalone component or in an NgModule.
The Angular team recommends using standalone components for all new development.
import { Component } from '@angular/core';
import { ProfilePhoto } from './profile-photo.component';
@Component({
standalone: true,
imports: [ProfilePhoto],
template: ` `
})
export class UserProfile { }
import { Component } from '@angular/core';
import { ProfilePhoto } from './profile-photo.component';
@Component({
standalone: true,
imports: [ProfilePhoto],
template: ` `
})
export class UserProfile { }
import { Component } from '@angular/core';
import { ProfilePhoto } from './profile-photo.component';
@Component({
standalone: true,
imports: [ProfilePhoto],
template: ` `
})
export class UserProfile { }
import { Component } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
@Component({
standalone: true,
selector: 'user-profile',
template: ` `,
imports: [ReactiveFormsModule]
})
export class UserProfile {
profileForm = new FormGroup({
name: new FormControl('')
});
}
An Angular module is a class decorated with @NgModule metadata.
@NgModule({
// applications modules, angular modules, libraries, etc.
imports: [],
// components, directives and pipes which should be made public
// 're-export' imported modules
exports: [],
// components, directives and pipes which belong to the module
// -> they can only be used in components templates inside this modules
declarations: [],
// services which should be useable by child modules and components
// details on providers and injection are in the services presentation
providers: []
})
export class TestModule { }
@NgModule({
imports: [CommonModule],
exports: [CommonModule, BookListComponent],
declarations: [BookListComponent, BookDetailComponent],
providers: [BookService],
})
export class BookModule { }
Application Structure Example with Modules
Structure with Standalone Components
Live Code Examples & Backup slides below
Following slides are not subject of the lecture.
Ask trainers in case of questions.
Their selectors only apply within the template of that component
Their class names and selectors do not collide with ones used elsewhere in the application
Component's styles cannot be changed by changes to styles elsewhere in the application
Pseudo selectors, e.g. :host, from shadow DOM style scoping are available
:host {
display: block;
font-size: x-large;
}
:host label {
font-weight: bold;
}
Angular Docs
Starting with Angular 19 standalone: true will be default.
@Component({
imports: [ProfilePhoto],
template: ` `
})
export class UserProfile { }
Add standalone: false to existing ngModule components so they continue to work.
@Component({
standalone: false,
template: ` `
})
export class UserProfile { }
Angular modules consolidate components, directives and pipes into cohesive blocks of functionality, each focused on a feature area, application business domain, workflow, or common collection of utilities.
AppModule is the top level Module which gets bootstrapped
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
// top level component to insert into index.html
bootstrap: [ AppComponent ]
})
export class AppModule { }
app.module.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
main.ts
<my-app>Loading ...</my-app>
index.html
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
})
export class AppComponent { }
app.component.ts