Can't Display my HTML content on TinyMCE Text Area

2.1k Views Asked by At

I am trying to create a rich text editor on my application's modal by using tinymce instead of text area. But my HTML code can't be displayed as a text on rich text's content area. I am using Angular 2.

Any help would be appriciated

import { Component, AfterViewInit, OnDestroy, Input, Output, EventEmitter } from 'angular2/core';
import { RdComponent, RdLib } from '../../../../../node_modules/mulberry/core';

declare let tinymce: any;

@Component({
  selector: 'mail-template',
  template: `
            <textarea style="height:15em"><p>{{ content }}</p></textarea>            
            `
})
export class MailTemplatesComponent extends RdComponent {

  @Input("rd-model") model: string;

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

  ngAfterViewInit() {
    console.log(this.model);
    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);
  }

}
<div class="col-md-12">
   <rd-field [rd-text]="translate('Mail İçeriği')"></rd-field>
   <mail-template [(rd-model)]="data.MailContent" rd-height="25em"></mail-template>
</div>

1

There are 1 best solutions below

1
Aseem Khan On

I am using angular2-froala-wyswiyg

this is the best i could find and easy to use (link below)

https://libraries.io/npm/angular2-froala-wyswiyg#usage

npm install angular-froala-wysiwyg --save
npm update froala-editor --save

package.json (file) after the installation of module

  "dependencies": {
    .....
    "angular-froala-wysiwyg": "^2.7.2",
    ...
}

Imports in your module

import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg';
...

@NgModule({
   ...
   imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot() ... ],
   ...
})

angular-cli.json(file) make changes in styles and scripts as shown below

 "styles": [
    "styles.css",
    "../node_modules/froala-editor/css/froala_editor.pkgd.min.css",
    "../node_modules/froala-editor/css/froala_style.min.css",
    "../node_modules/font-awesome/css/font-awesome.css"
  ],
  "scripts": [
    "../node_modules/froala-editor/js/froala_editor.pkgd.min.js"
  ],

In your component template

 <textarea class="form-control" [froalaEditor] name="x" #x="ngModel [(ngModel)]="obj.name" required ></textarea>

enter image description here After editing display it as

<span [innerHTML]="obj.name"> </span> 

enter image description here