服務器存儲的html文本綁定到contenteditable div時。該html不會被處理和渲染,而是呈現爲原樣。Angular2:Contenteditable div不能按預期工作
例如,來自服務器的html文本以下的文本呈現爲文本而不是html。
<br>---------- Forwarded message ----------<br>From: Someone <<a href="mailto:[email protected]" target="_blank">[email protected]</a>>
下面是當前的代碼:
HTML代碼:
<div class="description-input"
placeholder="Enter Description"
ion-content
contenteditable="true"
[(contenteditableModel)]="description">
</div>
的Javascript:
import {Directive, ElementRef, Input, Output, EventEmitter, OnChanges} from "@angular/core";
@Directive({
selector: '[contenteditableModel]',
host: {
'(blur)': 'onEdit()',
'(keyup)': 'onEdit()'
}
})
export class ContentEditableDirective implements OnChanges {
@Input('contenteditableModel') model: any;
@Output('contenteditableModelChange') update = new EventEmitter();
constructor(
private elementRef: ElementRef
) {
//console.log('ContentEditableDirective.constructor');
}
ngOnChanges(changes) {
//console.log('ContentEditableDirective.ngOnChanges');
//console.log(changes);
if (changes.model.isFirstChange())
this.refreshView();
}
onEdit() {
//console.log('ContentEditableDirective.onEdit');
var value = this.elementRef.nativeElement.innerText
this.update.emit(value)
}
private refreshView() {
//console.log('ContentEditableDirective.refreshView');
this.elementRef.nativeElement.textContent = this.model
}
}
可能的[angular 2 html binding]重複(http://stackoverflow.com/questions/31548311/angular-2-html-binding) – Abdel