How could I use ckeditor with Angular2 component?
Doing something like:
import {Component} from 'angular2/core'
@Component({
selector: 'e',
template: `
<textarea name="editor1" id="editor1" rows="10" cols="80">
This is my textarea to be replaced with CKEditor.
</textarea>
<script>
CKEDITOR.replace( 'editor1' );
</script>
`
})
export class E {
}
and printing it in index.html like:
<html>
<head>
<title>Hello</title>
<script src="../ckeditor/ckeditor.js"></script>
<script src="../systemjs/dist/system.src.js"></script>
<script src="../es6-shim/es6-shim.js"></script>
<script src="../angular2/bundles/angular2-polyfills.js"></script>
<script src="../rxjs/bundles/Rx.js"></script>
<script src="../angular2/bundles/angular2.dev.js"></script>
<script src="../angular2/bundles/router.dev.js"></script>
<script src="../angular2/bundles/http.dev.js"></script>
<script src="http://fgnass.github.io/spin.js/spin.min.js"></script>
<script>
System.config({
packages: {
compiled: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('../compiled/boot')
.then(null, console.error.bind(console));
</script>
</head>
<body>
Hackathon incoming!
<e>Loading...</e>
</body>
</html>
doesn't work. It works good when I put all the textarea with script code in index.html but I really want to have it in component template. It's like the ckeditor doesn't see the textarea ID when it is in component.
How can I make ckeditor plugin working good with Angular2 component? Thank you
Don't add the script to your template. use this instead
import {Component} from 'angular2/core'
declare const window: any;
@Component({
selector: 'e',
template: `
<textarea name="editor1" id="editor1" rows="10" cols="80">
This is my textarea to be replaced with CKEditor.
</textarea>
`
})
export class E {
constructor(){}
ngOnInit(){
if(window.CKEDITOR) {
window.CKEDITOR.replace('editor1');
}
}
}
UPDATE:
Check angular2 lifecycle hooks, and since the components are javascript you can execute any script code from inside your component.
A better way,
implement a CKEDITOR
component that will be used like this
<CKEditor [targetId]="editor1" [rows]="10" [cols]="80"> </CKEditor>
ckeditor.component.ts
import {Component, Input} from 'angular2/core';
declare const window: any;
@Component({
selector: 'CKEditor',
template: `
<textarea name="targetId" id="targetId" rows="rows" cols="cols">
This is my CKEditor component.
</textarea>
`
})
export class CKEditorComponent {
@Input() targetId;
@Input() rows = 10; //you can also give default values here
@Input() cols;
constructor(){}
ngOnInit(){
if(window.CKEDITOR) {
window.CKEDITOR.replace('editor1');
}
}
}
The best way,
Get the typescript definition file for CKEditor, I found it here. Add it to your project, after that you will be able to use the library.
this is only for illustrating, not tested.
import * as CKEDITOR from 'CKEDITOR/PATH';
.
.
ngOnInit(){
CKEDITOR.replace( targetId );
}