Angular ng2编辑器中的自定义函数

Angular ng2编辑器中的自定义函数,angular,typescript,ckeditor,Angular,Typescript,Ckeditor,我正在从事Angular 4项目,并在其中使用ng2 CkEditor。我已经覆盖了CkEditor的一些按钮的基本功能,如保存和预览,以执行我自己的功能。下面是我的代码 <ckeditor (ready)="OnReady()" [config]="editorConfig" formControlName="EditorNote" #CkEditor> <ckbutton

我正在从事Angular 4项目,并在其中使用ng2 CkEditor。我已经覆盖了CkEditor的一些按钮的基本功能,如保存和预览,以执行我自己的功能。下面是我的代码

<ckeditor  (ready)="OnReady()" [config]="editorConfig"  formControlName="EditorNote" #CkEditor>
                          <ckbutton
                            [name]="'customButton'"
                            [command]="'customCmd'"
                            [label]="'Click Me'"
                            [toolbar]="'CustomToolBar'"
                            [icon]="'https://avatars1.githubusercontent.com/u/5500999?v=2&s=16'">
                          </ckbutton>
                        </ckeditor>
PreviewOfferLetter()方法如下所示--

现在,当我点击“预览”按钮时,它会在控制台中抛出一个错误,上面写着--


如果我做错了什么,请提出建议。提前感谢。

您的语法正在丢失类的上下文。考虑切换到胖箭头,这样可以防止:

public OnReady() {
    this.CkEditor.instance.addCommand("source", {
      exec: () => {
          alert("Custom handler for source button ");
      }
    });
    this.CkEditor.instance.addCommand("preview", {
      exec: () => {
           this.PreviewOfferLetter()
      }
    });
  }
您还可以使用闭包,它更难看,但也可以工作:

public OnReady() {
    const self = this;
    this.CkEditor.instance.addCommand("source", {
      exec: function() {
          alert("Custom handler for source button ");
      }
    });
    this.CkEditor.instance.addCommand("preview", {
      exec: function() {
           self.PreviewOfferLetter()
      }
    });
  }

谢谢亲爱的,这真的很有效。非常感谢:)
this.PreviewOfferLetter is not a function
public OnReady() {
    this.CkEditor.instance.addCommand("source", {
      exec: () => {
          alert("Custom handler for source button ");
      }
    });
    this.CkEditor.instance.addCommand("preview", {
      exec: () => {
           this.PreviewOfferLetter()
      }
    });
  }
public OnReady() {
    const self = this;
    this.CkEditor.instance.addCommand("source", {
      exec: function() {
          alert("Custom handler for source button ");
      }
    });
    this.CkEditor.instance.addCommand("preview", {
      exec: function() {
           self.PreviewOfferLetter()
      }
    });
  }