【问题标题】:Unable to pass styles to quill-editor component in angular无法以角度将样式传递给 quill-editor 组件
【发布时间】:2019-08-23 22:17:58
【问题描述】:

我正在尝试在app.component.html 中设置app.component.ts 中的标签样式,但它不起作用。有类似的问题和文章,但即使是接受的答案也不适合我。这是我的代码。

app.component.html

<div class="container">
  <div class="row pt-5">
    <div class="col-md-12 col-lg-12 col-sm-12 bg-light">
      <form [formGroup]="editorForm" (ngSubmit)="onSubmit()">
        <div class="form-group">
          <label for="editor">
            <h3>Editor</h3>
          </label>
          <quill-editor [style]="editorStyle" [modules]="config" formControlName="editor"></quill-editor>
        </div>
        <button class="btn btn-primary mt-3">Submit</button>
      </form>
    </div>
  </div>
</div>

我的app.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  editorForm: FormGroup;

  editorStyle = {
    height: '300px',
    backgroundColor: '#ffffff'
  }

  config = {
    toolbar: [
      ['bold', 'italic', 'underline'],
      ['code-block']
    ]
  }

  ngOnInit() {
    this.editorForm = new FormGroup({
      'editor': new FormControl(null)
    })
  }

  onSubmit() {
    console.log('submit called');
    console.log(this.editorForm.get('editor').value);
  }
}

我尝试过的事情:

[style]="editorStyle" //不工作

然后我尝试直接在[style.backgroundColor]="'red'" 使用和不使用额外引号围绕值[style.backgroundColor]='red'

我也试过[ngStyle]="{backgroundColor: 'red'}"[ngStyle.backgroundColor]="'red'"'。但没有什么对我有用。问题仅在于editorStyle 而不是config。我也收到此警告。

警告:清理不安全的样式值 [object Object](请参阅 http://g.co/ng/security#xss)。

【问题讨论】:

  • quill-editor 组件的代码是什么?如果你有主机绑定,它们可以清除主机上的属性。

标签: angular quill property-binding


【解决方案1】:

Quill 编辑器使用自定义的styles 字段来传递样式,所以不是

<quill-editor [style]="editorStyle" [modules]="config" formControlName="editor"></quill-editor>

你应该指定

<quill-editor [styles]="editorStyle" [modules]="config" formControlName="editor"></quill-editor>

这与标准 dom 元素的样式不同

https://github.com/KillerCodeMonkey/ngx-quill

【讨论】:

【解决方案2】:

可能你有 shadow Dom 的问题,导致样式无法应用到 shadow dom 下的组件。更详细的解释在这里: https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM

Angular 有自己的方式来处理 shadow dom,而且是封装 https://angular.io/api/core/ViewEncapsulation

用这个修改你的app.component @Component注解并尝试一下

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  encapsulation: ViewEncapsulation.None
})

这是不可取的,因为 shadow dom 对于控制你的样式非常有用。在您的 app.component 中使用“无”封装可能会有风险,因此我认为更好的解决方案是将您的羽毛笔编辑器包装在一个自定义组件中,该组件的唯一责任是呈现编辑器。这样,您将 ViewEncapsulation.None 仅应用于此组件。

【讨论】:

  • 不,这不是继承问题,quill-editor 组件接收样式作为向下传递的数据输入(似乎无法正常工作)
【解决方案3】:

我给你试试这个:

您可以在此处进行编辑以纠正您的问题!

here

【讨论】:

  • 感谢萨德。但是heightbackgroundColor 属性对于我的文本编辑器没有改变。这就是问题所在。
  • 再看一遍我做了一些修改
猜你喜欢
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多