Angular 4. Add Ckeditor to component.ts

人走茶凉 提交于 2019-12-06 11:38:44

We have implemented the ck editor in our angular 4 project and faced same situation with other.

Below are the steps which will help you to have a clean implementation and don't have to put a link in index.html

add this line in your polyfills.ts

// used to programmatically set the configuration of the ckeditor file imports
// Note: this must stay as an import due to all of the imports happening before a statement is run
// no matter where the statement is in this file.
import './ckset';
// needed for our in place editing features
import 'ckeditor';

so in your src folder add the ckset.ts file now and put below content.

// needed for the configuration of ckeditor
(window as any).CKEDITOR_BASEPATH = '/assets/ckeditor/';

Your angular cli will now try to load the ckedit from asset folder and will fail if it will not get them. so have configure to add them to our asset folder during build process.

so add below code to your .angular-cli.json file.

  ...
  "outDir": "dist",
  "assets": [
    "assets",
    "favicon.ico",
    "configuration.json",
    {
      "glob": "{+(adapters|plugins|lang|skins)/**/*,config.js,styles.js,contents.css}",
      "input": "../node_modules/ckeditor",
      "output": "./assets/ckeditor"
    }
  ],
  "index": "index.html",
  ...

Your editer html will be almost same and make sure to add the extra plugin for divarera as iframe create other problems in angular4 in binding and all

   <div class="form-group">
      <ckeditor
        [(ngModel)]="text"
        [config]="{extraPlugins: 'divarea' }"
        formControlName="text"
        debounce="500">
      </ckeditor>
    </div>

And in your module make sure to include ng2-ckeditor module.

import { CKEditorModule } from 'ng2-ckeditor';

@NgModule({
  imports: [
    CKEditorModule,
    ...
  ],

yap, ofcourse you have to install ng2-ckeditor ;) first or add "ng2-ckeditor": "1.1.9", in your package.json

npm i --save ng2-ckeditor
Egor Doynikov

Thanks to Alex Beugnet and code from github.com/akveo/ng2-admin the solution was found!

1) npm install --save ckeditor (it will install the module with ckeditor library)

2) create ckeditor.loader.ts and add a loader at component's folder ( like here https://github.com/akveo/ng2-admin/blob/master/src/app/pages/editors/components/ckeditor/ckeditor.loader.ts)

ckeditor.loader.ts:

window['CKEDITOR_BASEPATH'] = '//cdn.ckeditor.com/4.6.0/full/'; 

3) import modules at cke-component.component.ts

import './ckeditor.loader';
import 'ckeditor';

4) remove all unused code Thanks!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!