Components
In this topic we will look at Angular Components and its metadata properties.
What is Angular Component ?
We can say Angular component is the main building block of angular application. The component contains data and logic which defines the behavior's and looks of the view. i.e. UI Page or HTML page.
Syntax to create angular component
ng generate component 'yourComponentName' or in short ng g c 'yourComponentName'.
The angular component are javascript classes which is defined using @Component Decorator. The Component is responsible to provide data to view or html. Angular does this by using Data Binding to get the data from component to view or html.
An angular application can have any number of components and each components plays there own role in a angular application.
The components contains three main building blocks:
1. Template
2. Class
3. Metadata
Template or view or HTML file
Templates are nothing but HTML code with Angular Template Syntax, without template there is nothing for angular to render. Data in template comes from component and in component it come from angular services. By the use of data binding techniques we can keep template and component in sync. Template uses Event Binding or Two Way Data Binding to notify component whether user has made some changes.
Two ways to specify template in Angular :
1. Defining the template inline.
2. Provide an external template.
Inline Templates ➤➤
We can specify template and styles inline using template and styles property as shown below
import { Component } from '@angular/core';
// @Component({
// selector: 'app-root',
// templateUrl: './app.component.html',
// styleUrls: ['./app.component.css']
// })
@Component({
selector: 'app-root',
template: '<h1> {{title}} works </h1>',
styles: ['h1 { font-weight: bold; }']
})
export class AppComponent {
title = 'SchoolManagementSystem';
}
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<h1> {{title}} works </h1>
<p> a long inline template </p>
`,
styles: ['h1 { font-weight: bold; }']
})
export class AppComponent {
title = 'SchoolManagementSystem';
}
Class contains properties and methods and by the use of Data Binding we can bind it to view or template. We can use typescript or javascript to create a class.
export class AppComponent {
title = 'SchoolManagementSystem';
}
Metadata
Metadata provides information to angular about the components and angular uses this information to process the class. @Component decorator is used to define component.
Important properties in metadata are selector, templateUrl, styleUrls
Selector ⮚ It specifies the CSS selector, where this template will be inserted in HTML. i.e. app-root.
templateUrl ⮚ This property contains the path of HTML template which is going to be displayed in the browser. i.e. app.component.html
styleUrls ⮚ This property contains the path of CSS file specific to this component.
Simple component code is shown below:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'SchoolManagementSystem';
}
Once angular component is created we have to register the component to angular modules as shown below:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
we have to import the component and then in declaration we have to add it. Add we want to load AppComponent when angular starts, we have add it in bootstrap array.
Comments
Post a Comment