Thursday, 14 September 2017

Inheritance - Angular 2

Angular 2.3 is released! One of the exciting new feature is component inheritance. Component inheritance is very powerful and it can increase your code reusability.

What does component inheritance provide us?

Component Inheritance in Angular 2.3 covers all of the following:

Metadata (decorators): metadata (e.g. @Input(), @Output), etc. defined in a derived class will override any previous metadata in the inheritance chain otherwise the base class metadata will be used.

Constructor: the base class constructor will be used if the derived class doesn’t have one, this mean that all the services you injected in parent constructor will be inherited to child component as well.

Lifecycle hooks: parent lifecycle hooks (e.g. ngOnInit, ngOnChanges) will be called even when are not defined in the derived class.

Component inheritance DO NOT cover templates and styles. Any shared DOM or behaviours must be handled separately.

Building with Component Inheritance

Let's start with a simple code. Here I will access the data from parent component to child component.

Use the Project that you have created before for Custom Directive.

1- Add a new folder named "Inheritance" in src/app directory.
2- Add two component, one is parent and other is child.

cd src\app\Inheritance
ng g c parent -is --flat --spec false
ng g c child -is --flat --spec false


3- Add a variable and a function in parent component.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styles: []
})
export class ParentComponent  {
protected company: string;
  constructor() { this.company="Dotnet guru";}

 public getHello()
 {
    alert("Hello Guru");
 }
}

4- Now extend the parent component in child component.
The super keyword can be used in child to reference parent class properties and the parent  class constructor.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import { Component, OnInit } from '@angular/core';
import{ParentComponent} from './parent.component';

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styles: []
})
export class ChildComponent extends ParentComponent {
  constructor() { super();}
}

5- Now you can access the parent component data in child component. see the code below in child.component.html

1
2
3
4
5
<p>
{{company}}
</p>

<button type="button" class="btn btn-lg btn-default" (click)="getHello()">button</button>

6- To run the page, bootstrap the ChildComponent in app.Module.ts file.

7- Add the selector of ChildComponent in index.html file.
<app-child></app-child>

8- Now save your Project and run from integrated terminal.
       ng serve <Enter>


0 comments:

Post a Comment

Topics

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (24) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (9) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (12) Web Api (10) Web Service (1) XMl (1)