How to override component in Angularjs

狂风中的少年 提交于 2021-01-27 11:23:14

问题


I want to override one of existing component in my custom module.

Here is the component -

import {SeComponent, TypedMap} from "smarteditcommons";
import {
GenericEditorField,
GenericEditorSanitizationService, IGenericEditor, SeRichTextFieldLocalizationService,
SeRichTextLoaderService
} from "smarteditcommons/components/genericEditor";
import "ckeditor";

@SeComponent({
templateUrl: 'seRichTextFieldComponentTemplate.html',
inputs: [
    'field:=',
    'qualifier:=',
    'model:=',
    'editor:=',
    'isDisabled'
]
})
export class SeRichTextFieldComponent {

public field: GenericEditorField;
public qualifier: string;
public model: TypedMap<any>;
public editor: IGenericEditor;

private mode: string;
private editorInstance: CKEDITOR.editor;

constructor(
    private seRichTextLoaderService: SeRichTextLoaderService,
    private seRichTextConfiguration: any,
    private genericEditorSanitizationService: GenericEditorSanitizationService,
    private seRichTextFieldLocalizationService: SeRichTextFieldLocalizationService,
    private $scope: angular.IScope,
    private $element: angular.IAugmentedJQuery
) {
    this.seRichTextLoaderService.load().then(() => {
        const textAreaElement = this.$element.find('textarea')[0] as HTMLTextAreaElement;
        this.editorInstance = CKEDITOR.replace(textAreaElement, this.seRichTextConfiguration);

        this.seRichTextFieldLocalizationService.localizeCKEditor();

        this.$element.bind('$destroy', () => {
            if (this.editorInstance && CKEDITOR.instances[this.editorInstance.name]) {
                CKEDITOR.instances[this.editorInstance.name].destroy();
            }
        });

        this.editorInstance.on('change', this.onChange.bind(this));
        this.editorInstance.on('mode', this.onMode.bind(this));
        CKEDITOR.on('instanceReady', this.onInstanceReady.bind(this));
    });
}
}

I want to override its constructor with some custom logic in my module. So i created same component with different name say CustomRichTextFieldComponent.

@SeComponent({
templateUrl: 'seRichTextFieldComponentTemplate.html',
inputs: [
    'field:=',
    'qualifier:=',
    'model:=',
    'editor:=',
    'isDisabled'
]
})
export class CustomRichTextFieldComponent extends SeRichTextFieldComponent {

public field: GenericEditorField;
public qualifier: string;
public model: TypedMap<any>;
public editor: IGenericEditor;

private mode: string;
private editorInstance: CKEDITOR.editor;

constructor(
    private seRichTextLoaderService: SeRichTextLoaderService,
    private seRichTextConfiguration: any,
    private genericEditorSanitizationService: GenericEditorSanitizationService,
    private seRichTextFieldLocalizationService: SeRichTextFieldLocalizationService,
    private $scope: angular.IScope,
    private $element: angular.IAugmentedJQuery
) {
     // custom changes
     super();
  }
}

To use this component in my module I am trying something like this ..

angular.module('customRichTextModule', []).component('seRichTextFieldComponent', {
controller: ['CustomRichTextFieldComponent']});

But its not working. What is the correct way to do ?


回答1:


If you want your Component two have a different behaviour in another Module, the right way is to inject a Service with a different value to that Module. Then read the value from component and decide what to do.

In your service:

@Injectable()
export class MyService {
 type;

 constructor(type){
  this.type = type;
 }
}

In your module (each module a differnt value in constructor):

  providers: [
    { provide: MyService , useValue: new MyService(1)},

And finaly in your component:

contructor(private service: MyService){
 if(service.type == 1){
  // do a different thing
 }
}


来源:https://stackoverflow.com/questions/62950970/how-to-override-component-in-angularjs

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