Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending a PrimeNg component inside Angular2

Due to some use cases, I need to extend a PrimeNG component in Angular2. For proofing purpouses, I chose using the DataTable since it is complicated enough.

After literally copy pasting the @Component annotation, I have added:

export class PPDataTable extends DataTable {}

At this point I get the following error: No provider for DataTable. If I add DataTable to the providers array in the annotation, the content of my DataTable is empty.

Therefore I tried adding: { provide: DataTable, useValue: PPDataTable } which yields in some errors such as : TypeError: co.dt.isSorted is not a function. I tried logging this.isSorted in the new class and it does exist.

How do I extend something like this?

Also, got any better solutions to change the selector name of a PrimeNg component ( wrap it somehow ) ?

Edit

After looking some more through the debug stack I found this:enter image description here

It seems that instead of providing directly the object, I am providing an array containing the object and this is (by my guess) the cause of the error. If I go in the sourcecode and change it to dt[0].isSorted() it works!

How do I provide the object directly?

Answer

Seems that if I provide { provide: DataTable, useExisting: PPDataTable } it works.

like image 838
taigi100 Avatar asked Jun 13 '17 10:06

taigi100


People also ask

Can we extend a component in Angular?

Typescript and Angular give you a way to handle this encapsulation. Inherited components! Using class inheritance in TypeScript, you can declare a base component that contains common UI functionality and use it to extend any standard component you'd like.

Can we extend two components in Angular?

If the abstract class extended by two or more components contains shared logic: use a service or even create a new typescript class that can be shared between the two components.

How do you test PrimeNG components?

PrimeNG provides vast UI components. The best way to test their integration into the application is to mock them with ng-mocks and verify that inputs / outputs have been bound correctly, and if a component supports templates, it received right ones. To test it, we need to: mock p-calendar.


1 Answers

Try this out my good friend:

import { Component, forwardRef } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { PrimeNgClassToExtend } from 'path';

const DATATABLE_VALUE_ACCESSOR: any = {
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => MyClassThatIsExtending),
    multi: true
};

@Component({
    selector: 'my-class-extending',
    providers: [DATATABLE_VALUE_ACCESSOR],
    template: ``
})
export class MyClassThatIsExtending extends PrimeNgClassToExtend {
}

We use NG_VALUE_ACCESSOR in order to connect your custom control to ngModel with Control Value Accessor.

Check this tutorial also, could help.

like image 155
SrAxi Avatar answered Sep 20 '22 05:09

SrAxi