Angular2 - CKEditor 使用
Angular2 - CKEditor use
如何将 ckeditor 与 Angular2 组件一起使用?
做类似的事情:
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 {
}
并在 index.html 中打印它,例如:
<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>
不起作用。当我将所有带有脚本代码的文本区域放在 index.html 中时效果很好,但我真的想将它放在组件模板中。这就像ckeditor在组件中时看不到textarea ID。
如何使 ckeditor 插件与 Angular2 组件一起正常工作?谢谢
不要将脚本添加到您的模板中。用这个代替
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');
}
}
}
更新:
检查 angular2 lifecycle hooks,由于组件是 javascript,您可以从组件内部执行任何脚本代码。
更好的方法,
实现一个 CKEDITOR
将像这样使用的组件
<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');
}
}
}
最好的方法,
获取CKEditor的typescript定义文件,我找到了here。
将其添加到您的项目中,之后您就可以使用该库了。
仅供说明,未经测试
import * as CKEDITOR from 'CKEDITOR/PATH';
.
.
ngOnInit(){
CKEDITOR.replace( targetId );
}
如何将 ckeditor 与 Angular2 组件一起使用?
做类似的事情:
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 {
}
并在 index.html 中打印它,例如:
<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>
不起作用。当我将所有带有脚本代码的文本区域放在 index.html 中时效果很好,但我真的想将它放在组件模板中。这就像ckeditor在组件中时看不到textarea ID。
如何使 ckeditor 插件与 Angular2 组件一起正常工作?谢谢
不要将脚本添加到您的模板中。用这个代替
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');
}
}
}
更新:
检查 angular2 lifecycle hooks,由于组件是 javascript,您可以从组件内部执行任何脚本代码。
更好的方法,
实现一个 CKEDITOR
将像这样使用的组件
<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');
}
}
}
最好的方法,
获取CKEditor的typescript定义文件,我找到了here。 将其添加到您的项目中,之后您就可以使用该库了。
仅供说明,未经测试
import * as CKEDITOR from 'CKEDITOR/PATH';
.
.
ngOnInit(){
CKEDITOR.replace( targetId );
}