Override components , like services?
Asked Answered
C

1

8

Say we import one of Angular Material modules :

 providers:[],
 imports : [MaterialInput]

Inside MaterialInput , there is a component used named : MaterialInputComponent

For some reasons I want to override that component with my own

So I want to be able to say :

 providers:[
    {
       provide: MaterialInputComponent,
       useClass : MyOwnInputComponent
    }
 ],
 imports : [MaterialInputModule]

I know we can override services like that, but can it be done for components or directives as well ?

UPDATE : I am not looking for Component inheritance, what I want is to use something like Material Module but sometimes I want to override some it's components behaviours, like you do with services.

Like :

If this is the original code behind MaterialInput component , which is in my node module.

  @Component({})
  export class OriginalMaterialInputComonent{
        greet(){ alert('Say Aloo'); }
  }

And I have a similar class like :

  @Component({})
  export class OverrideMaterialInputComonent{

        greet(){ alert('Say yes we can'); } // overriden function
  }

And, say I'm importing the hole MaterialInputModule

declarations:[
   {
    provide: OriginalMaterialInputComonent,
    useClass : OverrideMaterialInputComonent
  }
],
  imports : [MaterialInputModule]

Is that doable?

Codification answered 3/4, 2017 at 4:25 Comment(1)
Which version of Angular 2 are you using? Component inheritance is available with Angular 2.3Roughish
C
-1

You can use decorators for that, You can look into the below jsfiddle example

Overriding components

https://jsfiddle.net/mohan_rathour/sqv0jx21/

Conclude answered 13/4, 2017 at 6:4 Comment(4)
Please post relevant code here, rather than a link that may go dead in the futurePundit
@peeskillet That code is created by myself and will not dead in futureConclude
@mohanrathour is this AngularJS ? My question is in Angular2Codification
it's angular 1.5.8, but you can also use decorators into angular 2 as wellConclude

© 2022 - 2024 — McMap. All rights reserved.