Home >>Angular 6 Tutorial >Angular 6 Components

Angular 6 Components

Angular 6 Components

Significant part of the development of Angular 6 is performed in the components. Components are essentially classes that interact with the .html file of the component, which gets shown on the page. We have seen the file structure in one of our previous articles. The file structure has the app component and it consists of the following files –

  • app.component.css
  • app.component.html
  • app.component.spec.ts
  • app.component.ts
  • app.module.ts

The above files were generated by default when we built new project using the angular-cli command.

When you open up the app.module.ts file, it has several libraries which are imported and also a declarative which is allocated the appcomponent as follows –


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 { }

The statements contain the AppComponent variable, which we have already imported. This becomes the parent component.

Now, angular-cli has a command to build your own component. However, the app component which is generated by default will still remain the parent and the next components produced will form the child components.

Let us now run the command to build the component.

ng generate component new-cmp

When you run the above command in the command line, you can provide the following output –

D:\Node\Angular6App>ng generate component new-cmp
CREATE src/app/new-cmp/new-cmp.component.html (26 bytes)
CREATE src/app/new-cmp/new-cmp.component.spec.ts (629 bytes)
CREATE src/app/new-cmp/new-cmp.component.ts (272 bytes)
CREATE src/app/new-cmp/new-cmp.component.css (0 bytes)
UPDATE src/app/app.module.ts (398 bytes)

Now, if we go and check the file structure, we can get the new-cmp new folder generated under the src / app folder.

The following files are created in the new-cmp folder −

  • new-cmp.component.css − css file for the new component is created.
  • new-cmp.component.html − html file is created.
  • new-cmp.component.spec.ts − this may be used for unit testing.
  • new-cmp.component.ts − here, we will define the module, property, etc.

Changes are applied to the app.module.ts file as follows –


import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { NewCmpComponent } from './new-cmp/new-cmp.component';
// includes the new-cmp component we created
@NgModule({
   declarations: [
      AppComponent,
      NewCmpComponent 
   ],
   imports: [
      BrowserModule
   ],
   providers: [],
   bootstrap: [AppComponent] 
})
export class AppModule { }

The new-cmp.component.ts file is generated as follows −


import { Component, OnInit } from '@angular/core'; // here angular/core is imported .
@Component({
   // this is a declarator which starts with @ sign. The component word marked in bold needs to be the same.
   selector: 'app-new-cmp', //
   templateUrl: './new-cmp.component.html', 
   // reference to the html file created in the new component.
   styleUrls: ['./new-cmp.component.css'] // reference to the style file.
})
export class NewCmpComponent implements OnInit {
   constructor() { }
   ngOnInit() {}
}

If you see the above new-cmp.component.ts file, it introduces a new class named NewCmpComponent, which implements OnInit. In, which has a constructor and a method called ngOnInit(). ngOnInit is named by default while the class is executed.

When we hit the url in the http://localhost:4200/ browser, it first executes the index.html file which is shown below −


<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>Angular 6 Application</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>

The above is the standard html file and we do not see something that is written in the browser. Take a look at the tag in the body section.

<app-root></app-root>

This is the root tag created by the Angular by default. This tag has the reference in the main.ts file.


import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
   enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule);

Let us now see the app.module.ts file −


import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { NewCmpComponent } from './new-cmp/new-cmp.component';
@NgModule({
   declarations: [
      AppComponent,
      NewCmpComponent
   ],
   imports: [
      BrowserModule
   ],
   providers: [],
   bootstrap: [AppComponent]
})

export class AppModule { }

Here, the AppComponent is the name given, i.e., the variable to store the reference of the app. Component.ts and the same is given to the bootstrap. Let us now see the app.component.ts file.


import { Component } from '@angular/core';
@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   title = 'Angular 6 Project!';
}

Angular core is imported and referred as the Component and the same is used in the Declarator as −

@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})

In the declarator reference to the selector, templateUrl and styleUrl are given. The selector here is nothing but the tag which is put in the index.html file that we saw above.

The class AppComponent has a variable named title, which is shown in the browser.

The @Component uses the templateUrl called app.component.html which is as follows −


<!--The content below is only a placeholder and can be replaced.-->
<div style = "text-align:center">
   <h1>
      Hello {{title}}.
   </h1>
</div>

This has only the html code and the field description in curly brackets. It gets replaced with the value, which is present in the app.component.ts file. This is called binding. We may discuss the concept of binding in a related chapter.

Now that we have created a new component called new-cmp. The same gets included in the app.module.ts file, when the command is run for creating a new component.

new-cmp.component.ts


import { Component, OnInit } from '@angular/core';
@Component({
   selector: 'app-new-cmp',
   templateUrl: './new-cmp.component.html',
   styleUrls: ['./new-cmp.component.css']
})
export class NewCmpComponent implements OnInit {
   constructor() { }
   ngOnInit() {}
}

Here, we have to import the core too. The reference of the component is used in the declarator.

The declarator has the selector named app-new-cmp and the templateUrl and styleUrl.

The .html name new-cmp.component.html is as follows −

<p>new-cmp works!>/p>

The selector, i.e., app-new-cmp needs to be added in the app.component .html file as follows −

<!--The content below is only a placeholder and can be replaced.-->
<div style = "text-align:center">
   <h1>
      Hello {{title}}.
   </h1>
</div>
<app-new-cmp></app-new-cmp>

When the <app-new-cmp></app-new-cmp> tag is added, all that is present in the .html file of the new component created will get displayed on the browser along with the parent component data.

Let us see the new component .html file and the new-cmp.component.ts file.

new-cmp.component.ts


import { Component, OnInit } from '@angular/core';
@Component({
   selector: 'app-new-cmp',
   templateUrl: './new-cmp.component.html',
   styleUrls: ['./new-cmp.component.css']
})
export class NewCmpComponent implements OnInit {
   newcomponent = "Entered in new component created";
   constructor() {}
   ngOnInit() { }
}

The above variable is bound in the .new-cmp.component.html file as follows −

<p>
   {{newcomponent}}
</p>
<p>
   new-cmp works!
</p>

Similarly, we can create components and link the same using the selector in the app.component.html file as per our requirements.