Open In App

How to manually register a component in AngularJS ?

As we all know, components in angularJS are the basic building blocks of each application. Normally, we can create a component in angular by simply running the following command.

ng g c component_name

This creates a series of files as shown in the below figure. This includes the following files –



  1. HTML file: For generating HTML DOM (Document Object Model).
  2. spec.ts file: For running unit test cases of the component.
  3. component.ts: This is the main file that contains the main logic of the project.
  4. css file: For adding styles to the web page.

Angular CLI Component Creation

In the last part of component generation by Angular CLI, it just updates the app.module.ts file to include the newly generated component. If you observe closely, this step is just the same step as we have performed in step 3 shown below.

Steps to manually generate a component –



New Folder Creation




// This is our main component class called
// as 'CustomerComponent'
  
// Here we have imported 'Component' 
// module from Angular library
import { Component } from "@angular/core";
  
@Component({
  
    // This selector is used to refer
    // to the component in html
    selector: 'chinmay',
  
    /* Template or templateURL is used to 
    provide the HTML part which is to be 
    rendered into the browser 
    DOM(Document Object Model) */
    template: '<h1> Welcome to manual component creation</h1>',
  
    // CSS styles if any required can 
    // be specified here.
    styles: []        
    }
)
// Exporting the component class so 
// that it can be used to generate 
// relationships among the components
export class CustomerComponent {
}




/* For the first few lines, we will have 
to import certain Angular library modules 
So that we can run our project smoothly.*/
  
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
  
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
  
// Here we have imported our custom created component
import { CustomerComponent } from './customer/customer';
  
@NgModule({
    declarations: [
        AppComponent,
        CustomerComponent
    ],
    imports: [
        BrowserModule,
        AppRoutingModule
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }




<h1>Welcome to GeeksForGeeks !! </h1>
<!-- Placeholder HTML DOM -->
<!-- CustomerComponent selector HTML DOM -->
<chinmay></chinmay>

Project Output on Browser


Article Tags :