實現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