【问题标题】:TinyMCE & Angular 2: Setting The Editor's Content Based On @InputTinyMCE & Angular 2:基于@Input 设置编辑器的内容
【发布时间】:2017-09-09 20:14:48
【问题描述】:

tinymce 的新手,不确定 setContent(this.content) 方法的实际放置位置。我当前的版本导致我收到错误:

TypeError: null is not an object (evaluating 'body.nodeName') --- runTask — zone.js:170

角色对象是通过查询我的数据库的服务检索的,该服务工作正常。

我在我的 persona.component.html 中设置了这样的实例:

<app-tinymce
   [elementId]="'persona-footnotes'"
   (onEditorContentChange)="keyupHandler($event)"
   [content]="persona.footnotes"
   ></app-tinymce>

app-tinymce.component.ts:

import {
  Component,
  AfterViewInit,
  EventEmitter,
  OnDestroy,
  Input,
  Output
} from '@angular/core';
import 'tinymce';
import 'tinymce/themes/modern';
import 'tinymce/plugins/table';
import 'tinymce/plugins/link';
import 'tinymce/plugins/paste';
import 'tinymce/plugins/lists';
import 'tinymce/plugins/advlist';
import 'tinymce/plugins/code';

declare let tinymce: any;

@Component({
  selector: 'app-tinymce',
  templateUrl: './tinymce.component.html',
  styleUrls: ['./tinymce.component.scss']
})
export class TinymceComponent implements AfterViewInit, OnDestroy {
  @Input() elementId: String;
  @Input() content: String;
  @Output() onEditorContentChange = new EventEmitter();

  editor;

  ngAfterViewInit() {
    tinymce.init({
      selector: '#' + this.elementId,
      plugins: ['link', 'table', 'lists', 'advlist', 'code'],
      skin_url: '/assets/tinymce/skins/lightgray',
      toolbar: [
        'bold italic underline strikethrough subscript superscript removeformat | formatselect | fontsizeselect | bullist numlist outdent indent | link table | code'
      ],
      menubar:'edit',
      theme:'modern',
      height:'300',
      setup: editor => {
      editor.setContent(this.content);
      console.log(this.content); // this part outputs the correct data
        this.editor = editor;
        editor.on('keyup change', () => {
          const content = editor.getContent();
          this.onEditorContentChange.emit(content);
        });
      }
    });
  }

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

认为这是将 setContent(this.content) 方法放置在何处/“何时”的问题,但又不确定在何处?

【问题讨论】:

    标签: angular data-binding tinymce


    【解决方案1】:

    你很接近。您的设置函数需要延迟 setContent() 调用,直到编辑器被初始化。有一个事件,所以你可以尝试这样的事情:

    setup: editor => {
        this.editor = editor;
        editor.on('init', () => {
          editor.setContent(this.content);
        });
      }
    

    这将延迟对 setContent() 的调用,直到编辑器初始化并准备好使用 API 调用。

    【讨论】:

    • 谢谢!这消除了错误,但在显示的数据上仍然取得了一些间歇性的成功。我还添加了 *ngIf="content.property" 并且它得到了 100% 的显示时间(我的开发系统在响应时间上还有一些不足之处。)
    【解决方案2】:

    这是我的 TinyMCE 编辑器组件的完整代码。这可能会有所帮助。

    import { Component, AfterViewInit, OnDestroy, Input, Output, EventEmitter, ElementRef, provide, forwardRef, View } from 'angular2/core';
    import { RdComponent, RdLib } from '../../../../../node_modules/mulberry/core';
    
    declare let tinymce: any;
    
    @Component({
      selector: 'aril-mail-template',
      template: `<textarea style="height:25em"><p>{{model}}</p></textarea>`
    })
    
    export class MailTemplatesComponent extends RdComponent {
    
      @Input("rd-model") model;
      @Input("rd-default") default;
      @Input("rd-required") required;
      @Output("mail-template-save") mailTemplateSave: EventEmitter<any> = new EventEmitter<any>();
    
      editor;
    
      ngOnInit() {
        let that = this;
        tinymce.init({
          selector: 'textarea',
          height: "25em",
          menubar: true,
          plugins: [
            'advlist autolink lists link image charmap print preview anchor',
            'searchreplace visualblocks code fullscreen hr',
            'insertdatetime media table contextmenu paste spellchecker',
            'wordcount'
          ],
          toolbar: 'styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image spellchecker code',
          table_toolbar: "tableprops cellprops tabledelete | tableinsertrowbefore tableinsertrowafter tabledeleterow | tableinsertcolbefore tableinsertcolafter tabledeletecol",
          powerpaste_allow_local_images: true,
          powerpaste_word_import: 'prompt',
          powerpaste_html_import: 'prompt',
          spellchecker_language: 'en',
          spellchecker_dialog: true,
          content_css: [
            '//fonts.googleapis.com/css?family=Lato:300,300i,400,400i',
            '//www.tinymce.com/css/codepen.min.css'],
    
            setup: editor => {
              this.editor = editor;
              editor.on('init', () => {
                this.model && this.editor.setContent(this.model, {format: 'raw'});
              });
              editor.on('change', () => {
                const content = editor.getContent();
                this.mailTemplateSave.emit(content);
              });
            }      
        });    
      }
    
    
      ngOnDestroy() {
        tinymce.remove(this.editor);
      }
    
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多