一、使用angular-cli建立项目,安装froala editor 模块css
npm install angular2-froala-wysiwyg --save
二、配置app.module.ts文件html
import { FroalaEditorModule, FroalaViewModule } from 'angular2-froala-wysiwyg'; ... @NgModule({ ... imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot() ... ], ... })
三、配置 angular-cli.jsonnode
css "styles": [ "styles.css", "../node_modules/font-awesome/css/font-awesome.css", "../node_modules/froala-editor/css/froala_editor.pkgd.min.css" ],
js "scripts": [ "../node_modules/jquery/dist/jquery.min.js", "../node_modules/froala-editor/js/froala_editor.pkgd.min.js" ],
字体 "addons": [ "../node_modules/font-awesome/fonts/*.+(otf|eot|svg|ttf|woff|woff2)" ],
四、html界面配置jquery
<div [froalaEditor]>你好, Froala!</div>
没问题这样就配置完成了,可是咱们要作一些优化
一、配置图标,和其余项目
二、鼠标通过图标显示中文
三、添加组件,获取编辑器的值
四、将编辑器的值输出给父组件,使用@Output()npm
显示中文,angular-cli.json添加zh_cn.js文件json
"scripts": [ "../node_modules/jquery/dist/jquery.min.js", "../node_modules/echarts/dist/echarts.min.js", "../node_modules/froala-editor/js/languages/zh_cn.js" ],
获取编辑器的值(froalaText)angular2
<!-- [froalaEditor]="option" 这是配置项,若是没有本身定义option,那么就是默认配置 [(froalaModel)]="froalaText" 这是获取编辑器的值html --> <div [froalaEditor]="option" [(froalaModel)]="froalaText"></div>
配置图标,其余配置可参考官方文档app
language: "zh_cn", toolbarButtons: ['fullscreen', '|', 'bold', 'italic', 'underline', 'strikeThrough', 'align', 'insertLink', 'insertImage', 'insertHR', 'subscript', 'superscript'],
最后,编辑器确定是做为一个组件,来单独在其余组件中引用的,全部他的值要经过@Output() 传递给父组件,看下面简单的例子echarts
import {Component, Output, EventEmitter, OnInit} from "@angular/core"; @Component({ selector: "blog-froala", templateUrl: "../templates/froala.tpl.html" }) export class FroalaComponent implements OnInit { @Output() froala = new EventEmitter(); option:Object; froalaText:string; constructor() { this.froalaText = ""; } ngOnInit() { // 在事件中要使用外部的this,由于函数内部也有this因此讲this的值赋给that var that = this; // 参数配置 // https://www.froala.com/wysiwyg-editor/docs/options?utm_expid=98676916-2.gb-QgBReTCCS2F60oBIe_g.0&utm_referrer=https%3A%2F%2Fwww.google.com%2F#language this.option = { language: "zh_cn", //配置语言 placeholderText: "请输入内容", // 文本框提示内容 charCounterCount: true, // 是否开启统计字数 charCounterMax: 200, // 最大输入字数,目前只支持英文字母 // 注意导航条的配置, 按照官方的文档,没法配置,只能使用toolbarButtons来配置了。 toolbarButtons: ['fullscreen', '|', 'bold', 'italic', 'underline', 'strikeThrough', 'align', 'insertLink', 'insertImage', 'insertHR', 'subscript', 'superscript'], codeMirror: false, // 高亮显示html代码 codeMirrorOptions: { // 配置html代码参数 tabSize: 4 }, // 上传图片,视频等稳健配置 imageUploadURL:this.questionListService.IP+"sns/uploadPhoto",//GLOBAL.INCONFIG.getIP()+接口名称, //imageUploadURL:"http://11.177.50.63:9999/emanager/sns/uploadPhoto",//本地路径 imageUploadParams:{uid:this.questionListService.userInfo.id},//接口其余传参,默认为空对象{}, imageUploadMethod:"POST",//POST/GET, // 事件, 每次输入,就将值传递给父组件, 或者使用失去焦点的时候传递。 events: { 'froalaEditor.keyup': function (e, editor) { that.froala.emit(that.froalaText); console.log(editor.selection.get()); } } } } }
父组件中引用,获取编辑器的值 parent.html编辑器
<blog-froala (froala)="froalaContent($event)"></blog-froala>
父组件 parent.component.ts,组件中定义函数,这个函数会在子组件中触发,从而父组件获取子组件中编辑器的值。
froalaContent(event) { console.log(event) }