我是 angular 2 的新手,并试图将 tinymce 集成到 angular 2 中,但我无法在我的项目中使用 tinymce。到目前为止,我所做的是在我的项目中使用 bower 安装了 tinyMCe。所有 js 文件都成功添加到我的项目中。然后我在布局页面中添加了所有引用,如下所示:
<script src="~/lib/tinymce/tinymce.js"></script>
<script src="~/lib/tinymce/themes/modern/theme.js"></script>
<script src="~/lib/tinymce/plugins/link/plugin.js"></script>
<script src="~/lib/tinymce/plugins/paste/plugin.js"></script>
<script src="~/lib/tinymce/plugins/table/plugin.js"></script>
Run Code Online (Sandbox Code Playgroud)
在此之后,我编写了将使用 tineMce 的组件,如下所示:
import { Component, OnInit } from "@angular/core";
import { Routes, RouterModule } from "@angular/router";
import { NgForm } from "@angular/forms";
import Page = require("../../interfaces/iPage");
import PageService = require("../../services/page.service");
@Component({
//no need for selector as it will be loaded via routing
templateUrl: "/page/addEdit"
})
export class AddEditPageComponent implements OnInit {
model = this.newModel();
errorMessage: any;
tinymce: any;
constructor(private pageService: PageService.PageService) {
this.tinymce.init({
selector: "[tinymce]"
});
}
ngOnInit() {
}
newModel(): Page.IPage {
return {
pageId: 0,
pageName: null,
title: null,
content:null
};
}
submitForm(form: NgForm) {
debugger;
this.pageService.save(this.model).subscribe(model => {
this.model = this.newModel();
},
null);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在 html 上添加 textArea 如下:
<textarea class="form-control" name="model.content" [tinymce]="tinymce" style="height: 300px" [(ngModel)]="model.content" placeholder="Description"></textarea>
Run Code Online (Sandbox Code Playgroud)
当我不使用 tinYmce 时,我的页面工作正常,但是当我使用 tinyMCe 时,此错误会出现在吊屏屏幕上: 模板解析错误:无法绑定到“tinymce”,因为它不是“textarea”的已知属性.
如果我删除了 textarea 但不从 init 中删除 tinYmce 那么这个错误就会出现: TypeError: Cannot read property 'init' of undefined
我不知道我做错了什么。请帮忙。
要[tinymce]在您的 textarea 上使用,您必须将其声明为新的输入属性。
import { Input } from '@angular/core';
@Component({....})
export class AppComponent {
@Input() tinymce: string;
}
Run Code Online (Sandbox Code Playgroud)
TinyMCE 将需要一个有效的 css 选择器,您可能希望监听一些事件以支持实时绑定。
请参阅下面的完整实现
import { Component, AfterViewInit, OnDestroy, Output, EventEmitter } from '@angular/core';
declare var tinymce: any;
@Component({
selector: 'my-app',
template: `
<h3>Angular 2 Embedding TinyMCE</h3>
<textarea>Start Typing</textarea>
<p>{{ content }}</p>
`
})
export class AppComponent implements AfterViewInit, OnDestroy {
@Output() onEditorKeyup = new EventEmitter<any>();
public editor:any;
ngAfterViewInit() {
tinymce.init({
selector:'textarea',
setup: editor => {
this.editor = editor;
editor.on('keyup', () => {
const content = editor.getContent();
this.onEditorKeyup.emit(content);
})
}
});
}
ngOnDestroy() {
tinymce.remove(this.editor);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,在我的情况下,我已经从 CDN 加载了 tinymce,因此我不必设置主题文件。
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3787 次 |
| 最近记录: |