简体   繁体   English

如何在 Angular 材料中使用 ckeditor5

[英]How can I use ckeditor5 in Angular Material

I am trying to implement WYSIWYG editor in my angular application v10.我正在尝试在我的 angular 应用程序 v10 中实现 WYSIWYG 编辑器。 The editor is this .编辑器是这样的。 Now when I am implementing it on plane component(without angular material) it works fine.现在,当我在平面组件(没有 angular 材料)上实现它时,它工作正常。 The problem starts when I want it to implement it using Angular Material under mat-form-field.当我希望它使用 mat-form-field 下的 Angular Material 实现它时,问题就开始了。 Here is my code这是我的代码

<form class="example-form">
<mat-form-field class="example-full-width" appearance="outline">
  <mat-label>Request Title*</mat-label>
  <input matInput  value="">
</mat-form-field>
<mat-form-field class="mat-editor" appearance="outline">
  <mat-label>Request Description / Requirements*</mat-label>
 <ckeditor [editor]="Editor" data="<p>Hello, world!</p>"></ckeditor> 
 </mat-form-field> 

The Request Title input box appears fine but when it comes to Request Description field which is the WYSIWYG editor, it throws error in the console.请求标题输入框看起来很好,但是当涉及到作为所见即所得编辑器的请求描述字段时,它会在控制台中引发错误。 The error is mat-form-field must contain a MatFormFieldControl.错误是mat-form-field 必须包含 MatFormFieldControl。 When I just insert normal input box like below.当我像下面这样插入普通输入框时。 The editor is displayed but it looses focus as soon as I click on it and focus goes to the input box which i added.显示编辑器,但一旦我单击它就会失去焦点,焦点转到我添加的输入框。

<mat-form-field class="mat-editor" appearance="outline">
  <mat-label>Request Description / Requirements*</mat-label>
 <input matInput  value=""> 
  <ckeditor [editor]="Editor" data="<p>Hello, world!</p>"></ckeditor> 
 </mat-form-field> 

I even added the tag matInput in ckeditor like below, but it didn't work.我什至在 ckeditor 中添加了标签 matInput ,如下所示,但它不起作用。

  <ckeditor matInput   [editor]="Editor" data="<p>Hello, world!</p>"></ckeditor> 

I googled the problem and found one solution here .我用谷歌搜索了这个问题,并在这里找到了一个解决方案。 But it didn't solve my problem.但这并没有解决我的问题。

 <ckeditor matCkeditor   [editor]="Editor" data="<p>Hello, world!</p>"></ckeditor> 

This is how it is displayed when I add dummy textbox within tag.这就是我在标签中添加虚拟文本框时的显示方式。 But focus went to the dummy text box as soon as I click the editor to write something into it.但是,当我单击编辑器向其中写入内容时,焦点就转到了虚拟文本框。 Image图片

<mat-form-field appearance="outline" floatLabel="always" [hideRequiredMarker]="'true'">
  <mat-label>Message (please edit as needed)</mat-label>
  <textarea matInput #messageInput placeholder="Enter message" [(ngModel)]="model.message" required
    style="display: none;"></textarea>
  <ckeditor (focus)="onFocusElement()" (focusout)="onFocusoutElement()"
    [(ngModel)]="model.message" id="myckeditor1" #myckeditor1="ngModel" name="myckeditor1" [config]="CkeConfig"
    debounce="500">
  </ckeditor>
</mat-form-field>

on ts file在 ts 文件上

onFocusElement() {
    this.messageClass = 'msg-ckeditor';
  }

onFocusoutElement() {
    if (Utility.isNullorUndefined(this.model.message) || this.model.message.trim() === '') {
      this.messageClass = 'msg-ckeditor-error';
    } else {
      this.messageClass = '';
    }
  }

and on css side在 css 侧

.msg-ckeditor .mat-form-field-appearance-outline .mat-form-field-outline {
  color: #01a79d;
}
.msg-ckeditor-error .mat-form-field-appearance-outline .mat-form-field-outline {
  color: #f44336;
}

and it worked for me with ckeditor v1.2.6.它适用于 ckeditor v1.2.6。 If you find better way to solve than please share.如果您找到更好的解决方法,请分享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM