AngularJS - Module, Component and Template

Module

Angular application is composed of modules and every angular application will have at least one module in form of a class which is referred as the root module and conventionally it is named “AppModule” however, it’s not mandatory.

The class which represents the module is decorated with @NgModule directive.

Component

A component is part of a module which controls a view. Component is responsible for managing the view its responsible to using the template and data.

Template

It’s the view to of a component which will be visible to end user and will be used to interact with this component

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Nested component test</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
</body>
</html>





import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
 template:`<div style="text-align:center">
            <h1>
              Welcome to {{title}}!
            </h1>
            <p>
              <todaytime></todaytime>
            </p>
          </div>`,
})
export class AppComponent {
  title = 'app';
}



import {Component} from '@angular/core';

@Component({
    selector:"todaytime",
    template:`<h2>Today is {{today}}</h2>`
})
export class TodayTimeComponent{
    today=new Date();
}



import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import {TodayTimeComponent} from './TodayTimeComponent';

@NgModule({
  declarations: [
    AppComponent,TodayTimeComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

 

Tags