[英]Tiny Mce Two way Binding with Angular 2/4
This is my tinymce.component.ts
这是我的tinymce.component.ts
import {
Component,
OnDestroy,
AfterViewInit,
EventEmitter,
Input,
Output
} from '@angular/core';
@Component({
selector: 'simple-tiny',
template: `<textarea id="{{elementId}}"></textarea>`
})
export class SimpleTinyComponent implements AfterViewInit, OnDestroy {
@Input() elementId: String;
@Output() onEditorKeyup = new EventEmitter<any>();
editor;
ngAfterViewInit() {
tinymce.init({
selector: '#' + this.elementId,
plugins: ['link', 'paste', 'table'],
skin_url: 'assets/skins/lightgray',
setup: editor => {
this.editor = editor;
editor.on('keyup', () => {
const content = editor.getContent();
this.onEditorKeyup.emit(content);
});
},
});
}
ngOnDestroy() {
tinymce.remove(this.editor);
}
}
and now i am using it my html as under now i can get the text through the keyupHandlerFunction
but i want 2 way binding with ngModel
现在我正在使用我的html,因为现在我可以通过keyupHandlerFunction
获取文本,但是我想用ngModel
2种方式绑定
<simple-tiny
[elementId]="'my-editor-id'"
(onEditorKeyup)="keyupHandlerFunction($event)"
>
</simple-tiny>
This code is what the tinyMCE have suggested but i want ngModel
here for 2 way binding how can i do it here 这段代码是tinyMCE建议的,但是我想在这里ngModel
进行2种方式绑定,我该怎么办呢?
like: 喜欢:
<simple-tiny
[elementId]="'my-editor-id'"
(onEditorKeyup)="keyupHandlerFunction($event)"
[(ngModel)]="value">
</simple-tiny>
<p>{{ "My Model" + model}} </p>
You should implement ControlValueAccessor something like this: 您应该实现ControlValueAccessor这样的东西:
export const TINYMCE_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => SimpleTinyComponent),
multi: true
};
@Component({
selector: 'simple-tiny',
template: `<textarea #textArea [value]="value"></textarea>`,
providers: [TINYMCE_VALUE_ACCESSOR]
})
export class SimpleTinyComponent implements AfterViewInit,
OnDestroy, ControlValueAccessor {
@Input() elementId: String;
@ViewChild('textArea') textArea: ElementRef;
editor: any;
value: string;
onChange = (_: any) => { };
constructor(private zone: NgZone) {}
writeValue(value: any): void {
this.value = value;
if (this.editor) {
this.editor.setContent(value || '');
}
}
ngAfterViewInit() {
tinymce.init({
target: this.textArea.nativeElement,
plugins: ['link', 'paste', 'table'],
setup: editor => {
this.editor = editor;
editor.on('keyup', () => {
const content = editor.getContent();
this.zone.run(() => this.onChange(content))
});
}
});
}
registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }
registerOnTouched(fn: () => void): void { }
ngOnDestroy() {
tinymce.remove(this.editor);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.