gpt4 book ai didi

angular - Tinymce 与 Angular 2/4 的双向绑定(bind)

转载 作者:太空狗 更新时间:2023-10-29 17:48:04 25 4
gpt4 key购买 nike

这是我的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);
}
}

现在我正在使用我的 html,现在我可以通过 keyupHandlerFunction 获取文本,但我想要 2 种方式与 ngModel

绑定(bind)
<simple-tiny
[elementId]="'my-editor-id'"
(onEditorKeyup)="keyupHandlerFunction($event)"
>
</simple-tiny>

这段代码是 tinyMCE 建议的,但我想在这里使用 ngModel对于 2 种方式绑定(bind)我怎么能在这里做

喜欢:

<simple-tiny
[elementId]="'my-editor-id'"
(onEditorKeyup)="keyupHandlerFunction($event)"
[(ngModel)]="value">
</simple-tiny>

<p>{{ "My Model" + model}} </p>

最佳答案

你应该实现 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);
}
}

Stackblitz Example

Example inside form

关于angular - Tinymce 与 Angular 2/4 的双向绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46578932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com