我正在尝试从 ckEditor 获取更改后的值,即文本并将结果输出发送给父级。
下面是对应的代码:
editor.component.html:
<ckeditor tagName="textarea" [config]="config"
[data]="text" (change)="onValueChange($event)" formControlName="text"></ckeditor>
编辑器.component.ts
export class TextEditorWithLimitedWidgetsComponent implements OnInit, AfterViewChecked, OnChanges {
constructor(
private fb: FormBuilder,
private fileValidations: FileValidations,
private cdref: ChangeDetectorRef
) { }
@Input() text: string;
@Output() textValue = new EventEmitter();
form: FormGroup;
ngOnInit() {
this.form = this.fb.group({
text: ['', [
Validators.required,
CustomValidator.textEditor(30)
]]
});
this.form.setValue({
text: this.text
});
}
get f() {
return this.form.controls;
}
ngAfterViewChecked() {
// this.textValue.emit(this.form.controls);
// this.cdref.detectChanges();
//
// not working...
}
onValueChange(e) {
this.cdref.detectChanges();
}
ngOnChanges(changes: SimpleChanges): void {
this.textValue.emit(this.form.controls);
}
}
父组件.html
<app-editor [descriptionLimit]="50" [text]="inputData.title" (input)="(inputData.title = $event.target.value);" (textValue)="getTextValue($event)"></app-editor>
父组件.ts
getTextValue(event) {
const dataWithHTMLTags = event.text.value.toString();
this.inputData.title = this.fileValidations.stringsWithoutHTMLTags(dataWithHTMLTags);
console.log(this.inputData.title); // error..
}
我也尝试过ngAfterContentChecked但以同样的错误告终。