如何在 angular 2 中添加 TinyMce?

How to add TinyMce in angular 2?

我是 angular 2 的新手,正在尝试将 tinymce 集成到 angular 2 中,但我无法在我的项目中使用 tinymce。到目前为止,我所做的是在我的项目中使用 Bower 安装了 tinyMCe。所有 js 文件都已成功添加到我的项目中。然后我在布局页面中添加了所有引用,如下所示:

        <script src="~/lib/tinymce/tinymce.js"></script>
        <script src="~/lib/tinymce/themes/modern/theme.js"></script>
        <script src="~/lib/tinymce/plugins/link/plugin.js"></script>
        <script src="~/lib/tinymce/plugins/paste/plugin.js"></script>
        <script src="~/lib/tinymce/plugins/table/plugin.js"></script> 

在此之后,我编写了将使用 tineMce 的组件,如下所示:

import { Component, OnInit } from "@angular/core";
import { Routes, RouterModule } from "@angular/router";
import { NgForm } from "@angular/forms";
import Page = require("../../interfaces/iPage");
import PageService = require("../../services/page.service");
@Component({
    //no need for selector as it will be loaded via routing
    templateUrl: "/page/addEdit"
})


export class AddEditPageComponent implements OnInit {
    model = this.newModel();
    errorMessage: any;
    tinymce: any;

    constructor(private pageService: PageService.PageService) {
        this.tinymce.init({
            selector: "[tinymce]"
        });
    }

    ngOnInit() {

    }

    newModel(): Page.IPage {
        return {
            pageId: 0,
            pageName: null,
            title: null,
            content:null
        };
    }

    submitForm(form: NgForm) {
        debugger;
        this.pageService.save(this.model).subscribe(model => {
            this.model = this.newModel();
        },
            null);
    }


}

然后我在 html 上添加文本区域,如下所示:

<textarea class="form-control" name="model.content" [tinymce]="tinymce" style="height: 300px" [(ngModel)]="model.content" placeholder="Description"></textarea>

当我不使用 tinYmce 时我的页面工作正常,但是当我使用 tinyMCe 时,这个错误出现在吊屏上: 模板解析错误: 无法绑定到 'tinymce',因为它不是 'textarea' 的已知 属性。

如果我删除了 textarea 但没有从 init 中删除 tinYmce 那么就会出现这个错误: TypeError: 无法读取未定义的 属性 'init'

我不知道我做错了什么。请帮忙。

要在文本区域上使用 [tinymce],您必须将其声明为新输入 属性。

import { Input } from '@angular/core';
@Component({....})
export class AppComponent {
  @Input() tinymce: string;
}

TinyMCE 需要一个有效的 css 选择器,您可能想要监听一些事件以支持实时绑定。

见下文完整实施

import { Component, AfterViewInit, OnDestroy, Output, EventEmitter  } from '@angular/core';

declare var tinymce: any;

@Component({
  selector: 'my-app',
  template: `
            <h3>Angular 2 Embedding TinyMCE</h3>
            <textarea>Start Typing</textarea>
            <p>{{ content }}</p>
            `
})
export class AppComponent implements AfterViewInit, OnDestroy { 

  @Output() onEditorKeyup = new EventEmitter<any>();
  public editor:any;

  ngAfterViewInit() {
    tinymce.init({
      selector:'textarea',
      setup: editor => {
        this.editor = editor;
        editor.on('keyup', () => {
          const content = editor.getContent();
          this.onEditorKeyup.emit(content);
        })
      }
    });
  }

  ngOnDestroy() {
    tinymce.remove(this.editor);
  }

}

请注意,在我的例子中,我已经从 cdn 加载了 tinymce,所以我不必设置主题文件。

<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>