Angular 4: Component Example

Welcome to the new concept of components in angular 4.

Definition: component is a angular class , Components are the most basic building block of an UI in an Angular application. An Angular application is a tree of Angular components.

Components always have a template and only one component can be instantiated per an element in a template.
A component must belong to an NgModule in order for it to be usable by another component or application. To specify that a component is a member of an NgModule, you should list it in the declarations field of that NgModule In app.module file.


First We need to import the angular Component from the Angular package: @angular/core

import { Component } from ‘@angular/core’;

we will use this component with the component decorator @component

component decorator must have the selector and template.

Selector : css selector that identifies this component in a template. means we can use the html templates with selector in the application.

selector: ‘app’,

Template:  inline-defined template for the view. Means it’s the html code, we can write html code in the template, or we can directly give the html src by giving the template url.

template: `{{title}}`,

TemplateUrl:  Template url is the external file containing a html template for the view.

templateUrl: ‘./app.component.html’

Same as styles: css styles we can add two ways by adding styles, styleUrls.

Style: nline-defined styles to be applied to this component’s view

styles: [‘h1 { font-weight: normal; }’]

styleUrls: list of urls to stylesheets to be applied to this component’s view. If we have multiple styles we can add by the external css files.

styleUrls: [‘./app.component.css ]

Component Structure:

import { Component } from ‘@angular/core’;
selector: ‘app’,
template: ‘Hello <h4>{{title}}</h4>!’,

styles: [‘h4 { font-weight: normal; }’]
export class welcome {    title: string = ‘World’;     }


Every Angular app has at least one NgModule class, the root module, conventionally named AppModule lar apps are modular and Angular has its own modularity system called NgModules. We will add the components in the declarations of this module file.

Import statement  for the module:

import { NgModule } from ‘@angular/core’;

NgModule is a decorator to use the ng module.

most important properties are:

  • declarations– the view classes that belong to this module. Angular has three kinds of view classes: componentsdirectives, and pipes.
  • exports– the subset of declarations that should be visible and usable in the component templates of other modules.
  • imports– other modules whose exported classes are needed by component templates declared in this
  • providers– creators of services that this module contributes to the global collection of services; they become accessible in all parts of the app.

bootstrap – the main application view, called the root component, that hosts all other app views. Only the root module should set this bootstrap property.

Module  Structure:

import { NgModule } from ‘@angular/core’;
import { BrowserModule } from ‘@angular/platform-browser‘;
imports: [ BrowserModule ],
providers: [ Logger ],
declarations: [ AppComponent ],
exports: [ AppComponent ],
bootstrap: [ AppComponent ]

export class AppModule { }

The module declares some objects to be public by marking them with the export key word.


Launch an application by bootstrapping its root module. During development you’re likely to bootstrap the AppModule in a main.ts file like this one

import { platformBrowserDynamic } from ‘@angular/platform-browser-dynamic’;
import { AppModule } from ‘./app/app.module’;

Source code present in the Github 

Published on Web Code Geeks with permission by Ramesh Kotha, partner at our WCG program. See the original article here: Angular 4: Component Example

Opinions expressed by Web Code Geeks contributors are their own.

Ramesh Kotha

Ramesh is a certified Java professional working in financial domain, He is an application developer and has hands on practice with Java and related technologies. He is passionate about Spring and other open source technologies.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Eva M
Eva M
6 years ago

Very poor english. How come the WCG team let this be published as is ?

Back to top button