Angular 7具有innerHTML的反应式表单



我在Angular 7应用程序中使用Priming编辑器。

<p-editor formControlName="description" [style]="{'height':'320px'}"></p-editor>

我正在使用patchValue设置控件的值,如:

const desc = '<strong>Hello World</strong>';
this.form.patchValue({
description: desc
});

另一个用例:

const desc = '<ul><li>Test 1</li><li>Test 2</li><li>Test 3</li></ul>'

在上述情况下,p编辑器应将其呈现为:

  • 测试1
  • 测试2
  • 测试3

但我的被动表单删除了所有HTML格式,只为控件分配了"Hello World"。我需要保留来自服务器的HTML格式。

任何帮助都将不胜感激。

找不到合适的解决方案,但您可以尝试此破解:

<p-editor #myEditor formControlName="description" [style]="{'height':'320px'}"></p-editor>

和ts文件中的

import { Editor } from 'primeng/primeng';
@ViewChild('myEditor') myEditor: Editor

const desc = '<strong>Hello World</strong>';
this.form.patchValue({
description: desc
});
// this will replace innerHTML with service data
this.myEditor.e1.nativeElement.querySelector('ql-editor').innerHTML = desc;

最新更新