Angular Component: Building Blocks of A Component
Angular Component: Building Blocks of A Component
md 10/2/2018
Angular Component
Angular follows a clean modular architecture . A complex Angular web application user interface can be
divided as- .
MODULES : are the basic building blocks in an angular application .Modules logically group one or
more components and provide a compilation context for it.
This chapter discusses how to create and use components in an Angular 6 application.
Angular splits complex application UI into reusable components . So we can think of an angular application as
nothing but a bunch of components as shown in the diagram below.Every angular application must have at
least one component .Components work together in harmony to provide great user experience.
1/4
AngularComponent.md 10/2/2018
1. Template :is created using HTML. It acts an interface to accept user input.
2. Class :is created with Typescript.This defines the functionality of a component. It exposes methods and
properties of a component.
3. Metadata:It binds a template with a class. It is created using decorator function.We will use
@Component() decorator
message:String;
constructor(){
this.message = "TutorialsTeacher says ,Welcome to Angular6 Components "
}
}
Create a template file TutsTeacherHelloComponent.html. The h1 tag embeds the value of the
message property in the TutsTeacherHelloComponent class.
<section>
<h1>{{message}}</h1>
</section>
Add metadata to the class created . The decorator function @Component() is used to bind the
template and the class.
@Component({
2/4
AngularComponent.md 10/2/2018
templateUrl:'TutsTeacherHelloComponent.html',
selector:'tt-hello'
})
export class TutsTeacherHelloComponent {
message:String;
constructor(){
this.message = "TutorialsTeacher says ,Welcome to Angular6 Components "
}
}
The @Component() function takes in an object {} as parameter . The common properties of the object is
shown below
Sr
Property Description
No
The URL of a template file for an Angular component. If provided, do not supply an
1 templateUrl
inline template using template.
The CSS selector that identifies this directive in a template and triggers instantiation
2 selector
of the directive
3 styleUrls One or more URLs for files containing CSS stylesheets to use in this component
@NgModule({
declarations: [
TutsTeacherHelloComponent // register here
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [TutsTeacherHelloComponent] //modify here
3/4
AngularComponent.md 10/2/2018
})
export class AppModule { }
The Angular CLI creates an index.html page . To execute and view how the component looks in the
browser .We need to use the selector tt-hello in index.html as below.
<body>
<tt-hello></tt-hello>
</body>
Finally run the application with angular cli use ng serve command .Open the browser type url
http://localhost:4200/ and verify the output as below.
4/4