Extending a PrimeNg component inside Angular2

*爱你&永不变心* 提交于 2019-12-01 06:51:44

问题


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:

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.


回答1:


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.



来源:https://stackoverflow.com/questions/44519015/extending-a-primeng-component-inside-angular2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!