ngx quill切换语言后占位符不更新问题求助
Hey there! I’ve run into similar quirks with older ngx-quill versions and translation pipes, so let’s break down why this is happening and how to fix it.
Why This Happens
ngx-quill@1.6.0 initializes the Quill editor instance when your component first loads, and it doesn’t automatically recheck or update the placeholder attribute after the initial render. Even though your translation pipe updates the value behind the scenes, the Quill editor doesn’t pick up on that change because it’s not set to observe input property updates.
Solution 1: Manually Update Placeholder on Language Change
This is the most reliable fix for your setup. We’ll listen for language switch events and directly update the Quill editor’s placeholder value.
Step 1: Add a Template Reference to Your Quill Editor
First, give your editor a reference so we can access it in the component code:
<quill-editor #quillEditor [placeholder]="'Topic.new.quill-placeholder' | translate" <!-- rest of your editor configuration --> ></quill-editor>
(I switched to property binding [placeholder] instead of interpolation here — it’s slightly better for change tracking in Angular 4.)
Step 2: Listen for Language Changes and Update Quill
In your component, inject the TranslateService, grab the editor reference, and subscribe to language change events:
import { Component, ViewChild, OnInit, OnDestroy } from '@angular/core'; import { TranslateService } from 'ng2-translate'; import { QuillEditorComponent } from 'ngx-quill'; import { Subscription } from 'rxjs/Subscription'; @Component({ selector: 'your-component-selector', templateUrl: './your-component.component.html' }) export class YourComponent implements OnInit, OnDestroy { @ViewChild('quillEditor') private quillEditor: QuillEditorComponent; private langChangeSub: Subscription; constructor(private translate: TranslateService) {} ngOnInit() { // Subscribe to language change events this.langChangeSub = this.translate.onLangChange.subscribe(() => { // Fetch the latest translated placeholder text this.translate.get('Topic.new.quill-placeholder').subscribe(placeholderText => { // Update the Quill editor's placeholder directly if (this.quillEditor?.quillEditor) { this.quillEditor.quillEditor.root.dataset.placeholder = placeholderText; } }); }); } ngOnDestroy() { // Clean up the subscription to avoid memory leaks if (this.langChangeSub) { this.langChangeSub.unsubscribe(); } } }
Quill stores the placeholder text in the root element’s dataset.placeholder property, so updating that directly will reflect the new translated text immediately.
Solution 2: Force Change Detection (If Needed)
If the first solution alone doesn’t work (sometimes Angular’s change detection misses the pipe update), add ChangeDetectorRef to trigger a manual check:
import { ChangeDetectorRef } from '@angular/core'; // Add to your constructor constructor(private translate: TranslateService, private cdr: ChangeDetectorRef) {} // Inside the langChange subscription this.translate.get('Topic.new.quill-placeholder').subscribe(placeholderText => { this.cdr.detectChanges(); // Force Angular to update the bound value if (this.quillEditor?.quillEditor) { this.quillEditor.quillEditor.root.dataset.placeholder = placeholderText; } });
Key Notes
- ngx-quill@1.6.0 is quite old, so avoid upgrading to newer versions without double-checking compatibility with Angular 4.4.6.
- Always clean up subscriptions in
ngOnDestroyto prevent memory leaks in your application.
内容的提问来源于stack exchange,提问作者Ben Van Daele

