我需要将编辑器与我的项目angular2集成起来。
我选择了tinymce,但我不知道如何安装它
我发现了这样的东西:
tinymce.init({
selector: '.tinymce-editor',
schema: 'html5',
});
发布于 2016-06-16 04:26:42
使用ngForm、ControlGroup、NgZone:实现指令,如下所示
import {ElementRef, Directive, NgZone} from '@angular/core';
import {ControlGroup, Control} from '@angular/common';
@Directive({
inputs: ['tinyMce'],
selector: '[tinyMce]'
})
export class TinyEditor {
public tinyMce: ControlGroup;
private zone: NgZone;
private id: string = Math.random().toString(36).substr(2, 5);
private controlName: string;
private theControl: Control;
public constructor(private elRef: ElementRef, private zone: NgZone) {
}
public ngOnInit(): void {
this.zone.runOutsideAngular(() => {
this.controlName = this.elRef.nativeElement.getAttribute('ngControl');
this.theControl = this.tinMce.find(this.controlName);
this.elRef.nativeElement.setAttribute('tiny-id', id);
});
}
public ngAfterViewInit(): void {
this.zone.runOutsideAngular(() => {
tinymce.init({
valid_elements: '*[*]',
selector: '[tiny-id=' + this.id + ']',
schema: 'html5',
height: 400,
setup: (editor): void => {
editor.on('keyup change', () => {
this.zone.run(() => {
let value: Object = editor.getContent();
this.theControl.updateValue(value, {emitEvent: true});
this.theControl.markAsDirty();
this.theControl.markAsTouched();
this.theControl.updateValueAndValidity();
});
});
}
});
});
}
public ngOnDestroy(): void {
try {
tinymce.remove('[tiny-id=' + this.id + ']');
} catch(e) {
console.error('Error:', e);
}
}
}
使用:
<form #f="ngForm">
<input [tinyMce]="f" ngControl="valueHolder">
</form>
这样,您也可以在一个页面上拥有任意数量的实例。
https://stackoverflow.com/questions/37855066
复制