gpt4 book ai didi

angular - CkEditor 和 Angular Material 2 选项卡

转载 作者:行者123 更新时间:2023-12-03 21:29:50 24 4
gpt4 key购买 nike

我使用 angular-cli、angular-material 2 并尝试使用 CKEditor (ng2-ckeditor)。

当我将 CkEditor 直接插入 HTML(在一个 div 内)时,一切正常。

但是当我在以下结构下移动 CkEditor 代码时:

<md-tab-group>
<md-tab label="Another label description">
Contents 1
</md-tab>
<md-tab label="Label description">
<div>
<md-card>
<md-card-header>
<md-card-title>
This is a title
</md-card-title>
</md-card-header>
<md-card-content>
<ckeditor name="htmlTextValue"
[(ngModel)]="ckeditorContent"
[config]="{uiColor: '#99000'}"
(change)="onChange($event)"
(ready)="onReady($event)"
(focus)="onFocus($event)"
(blur)="onBlur($event)"
debounce="500">
</ckeditor>
</md-card-content>
</md-card>
</div>
</md-tab>
</md-tab-group>

它失败了,这是来自控制台的堆栈跟踪:
ERROR TypeError: Cannot read property 'unselectable' of null
at b (ckeditor.js:331)
at a.<anonymous> (ckeditor.js:327)
at a.m (ckeditor.js:10)
at a.CKEDITOR.event.CKEDITOR.event.fire (ckeditor.js:12)
at a.CKEDITOR.editor.CKEDITOR.editor.fire (ckeditor.js:13)
at a.fireOnce (ckeditor.js:12)
at a.CKEDITOR.editor.CKEDITOR.editor.fireOnce (ckeditor.js:13)
at Object.<anonymous> (ckeditor.js:251)
at e (ckeditor.js:231)
at Object.load (ckeditor.js:231)

以下是 package.json 中的重要信息:
"dependencies": {
"@angular/animations": "^4.1.1",
"@angular/common": "^4.1.1",
"@angular/compiler": "^4.1.1",
"@angular/core": "^4.1.1",
"@angular/forms": "^4.1.1",
"@angular/http": "^4.1.1",
"@angular/material": "^2.0.0-beta.3",
"@angular/platform-browser": "^4.1.1",
"@angular/platform-browser-dynamic": "^4.1.1",
"@angular/router": "^4.1.1",
"core-js": "^2.4.1",
"hammerjs": "^2.0.8",
"ng2-ckeditor": "^1.1.8",
"rxjs": "^5.1.0",
"zone.js": "^0.8.4"
},
"devDependencies": {
"@angular/cli": "1.0.2",
"@angular/compiler-cli": "^4.1.1",
"@types/hammerjs": "^2.0.34",
"@types/jasmine": "2.5.38",
"@types/node": "~6.0.60",
"codelyzer": "~2.0.0",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "~2.0.0",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^0.2.0",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.0",
"ts-node": "~2.0.0",
"tslint": "~4.5.0",
"typescript": "~2.2.0"
}

这是我的组件的代码:
  ckeditorContent = 'This is a text';

onChange($event) {
console.log('Change');
console.log($event);

}

onReady($event) {
console.log('OnReady');
console.log($event);

}

onFocus($event) {
console.log('Focus');
console.log($event);
}

onBlur($event) {
console.log('Blur');
console.log($event);
}

我也尝试了 ckeditor builder 以添加 divarea 插件,但没有运气。

不幸的是,我无法为此创建一个 plunker,因为现在不支持 angular-cli。

你知道为什么吗?
先感谢您。

最佳答案

我通过使用这个技巧(脏修复但工作)解决了这个问题。

首先,在您的 HTML 模板中装饰 ckeditor,使其仅在方法 isVisible() 返回 true 时可见:

<ng-container *ngIf="isVisible()">
<ckeditor data="" [config]="ckEditorConfig"
(dataChange)="newData($event)" debounce="500"></ckeditor>
</ng-container>

然后在您的 TypeScript 中,添加方法 isVisible() 如下。此方法需要您注入(inject) ElementRef(将其添加到组件构造函数中):
constructor(
// add this line and import ElementRef:
// import { ElementRef } from '@angular/core';
private elementRef: ElementRef
) {
// keep any existing code that exists
}

isVisible(): boolean {
const element = this.elementRef.nativeElement;
return element.offsetParent !== null;
}

这样做,CKEditor 将仅在组件可见时被实例化(假设当组件可见时,它已完成加载)。

请注意,使用 ElementRef 会使您的应用程序更容易受到攻击(请参阅: https://angular.io/api/core/ElementRef )

关于angular - CkEditor 和 Angular Material 2 选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44574696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com