实现TinyMCE的我一直在关注这个视频:https://www.youtube.com/watch?v=0IXelrVEoWI和corrosponding github上源:https://github.com/tabvn/angular-blog与angular2
它完美地创造新的内容,但我'尝试加载和编辑现有的内容时面临的一些问题在同一个编辑器中。
我的HTML DOM元素看起来是这样的:
<div class="row">
<div class="form-group col-lg-10">
<label for="description">description:</label>
<text-editor [value]="defaultBodyValue" [elementId]="'description'" (onEditorKeyup)="onBodyTextEditorKeyUp($event)"></text-editor>
</div>
</div>
在我的部分,我把这种方法从ngOnInit():
getDescription(): void {
let id;
this.route.params.forEach((params: Params) => {
id = +params['id'];
})
this.descService.getDesc(id).then(desc => this.desc = desc);
this.defaultBodyValue = "asd";
}
我硬编码的描述为 “ASD” 为测试目的。
我editor.component.ts来源是这样的:
export class EditorComponent implements OnInit, AfterViewInit, OnDestroy, OnChanges {
@Input() elementId: string;
@Input() value: any = "";
@Output() onEditorKeyup: EventEmitter<any> = new EventEmitter<any>();
baseURL: string = '/';
constructor() {
}
ngOnInit() {
}
editor;
ngAfterViewInit() {
tinymce.init({
selector: '#' + this.elementId,
plugins: ['link', 'paste', 'table', 'lists', 'hr', 'emoticons', 'advlist'],
skin_url: this.baseURL + 'assets/skins/lightgray',
setup: editor => {
this.editor = editor;
editor.on('keyup',() => {
const content = editor.getContent();
this.onEditorKeyup.emit(content);
});
},
});
}
ngOnDestroy() {
tinymce.remove(this.editor);
}
didSetValue: boolean = false;
ngOnChanges(){
console.log(this.value);
console.log("TEST: " + this.editor);
if(!isNullOrUndefined(this.editor) && this.value !== "" && !this.didSetValue){
console.log(this.value);
this.didSetValue = true;
this.editor.setContent(this.value);
}
}
}
创建新内容时,它完美,但当我尝试编辑内容时,它会记录下到控制台:
asd
editor.component.ts:55 TEST: undefined
修订ngAfterViewInit()函数:
ngAfterViewInit() {
tinymce.init({
selector: '#' + this.elementId,
plugins: ['link', 'paste', 'table', 'lists', 'hr', 'emoticons', 'advlist'],
skin_url: this.baseURL + 'assets/skins/lightgray',
setup: editor => {
editor = editor;
editor.on('keyup',() => {
const content = editor.getContent();
this.onEditorKeyup.emit(content);
});
},
});
}
喜迈克尔,我已经更新了原来的问题的方法。不幸的是,它并没有改变结果。它仍然没有定义。改变你的意思了吗? – Tony
你可以在JSFiddle上创建一个示例页面,以便我们可以看到运行代码?用部分代码中的'this'解决范围问题是非常困难的。 –
我尝试过迈克尔,但是我发现它复制起来相当复杂...... :(现在(从你在哪里用this.editor说起)的启发我尝试使用类似于:ngOnChanges(){ console .LOG(THIS.VALUE); 的console.log(TinyMCE的);如果 (!isNullOrUndefined(this.editor)&& THIS.VALUE == “” && this.didSetValue!){ 的console.log(这一点。值); 这个。didSetValue = true; //this.editor.setContent(this.value); //tinymce.get(tinymce.editors[1].id).setContent(this.value); tinymce.get(tinymce.editors); } tinymce.setContent = this.value; } – Tony