Angular封裝WangEditor富文本組件的方法
富文本組件是web程序中很常用的一個組件,特別是要開發(fā)一個博客,論壇這類的網(wǎng)站后臺。
得益于Angular的強(qiáng)大,封裝WangEditor組件非常簡單
1.使用yarn或者npm安裝wangeditor
yarn add wangeditor
2.創(chuàng)建一個Angular的組件
ng g c q-wang-editor
3.封裝組件邏輯
3.1 模板
<div #wang></div>
3.2 ts邏輯
import { Component, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output, ViewChild, ViewEncapsulation } from '@angular/core';
import { ControlValueAccessor } from '@angular/forms';
import E from "wangeditor"
import hljs from 'highlight.js'
import "node_modules/highlight.js/styles/xcode.css"
@Component({
selector: 'q-wang-editor',
templateUrl: './q-wang-editor.component.html',
styleUrls: [
'./q-wang-editor.component.less',
'../../../../../node_modules/highlight.js/styles/xcode.css'],
encapsulation: ViewEncapsulation.None,
})
export class QWangEditorComponent implements OnInit, ControlValueAccessor,OnDestroy {
@ViewChild("wang")
editor!: ElementRef;
@Input() value: string = '';
@Input() height = 300;
@Output() valueChange = new EventEmitter();
onChange: ((value: string) => {}) | undefined;
html = ''
wangEditor: E | undefined;
constructor() { }
ngOnDestroy(): void {
this.wangEditor?.destroy();
}
writeValue(obj: any): void {
this.html = obj;
this.wangEditor?.txt.html(this.html)
}
registerOnChange(fn: any): void {
}
registerOnTouched(fn: any): void {
}
ngOnInit(): void {
setTimeout(() => {
this.wangEditor = new E(this.editor.nativeElement)
this.wangEditor.config.zIndex = 500;
this.wangEditor.config.height = this.height
this.wangEditor.highlight = hljs;
this.wangEditor.config.onchange = (html: any) => {
this.valueChange.emit(html)
if (this.onChange) {
this.onChange(html);
}
}
this.wangEditor.config.onchangeTimeout = 500;
this.wangEditor.create();
this.wangEditor.txt.html(this.html)
}, 200);
}
}
大致思路:
- 使用ViewChild引用html的dom元素
- 在OnInit的成功后,初始化WangEditor編輯器,把模板中的ElementRef放入到WangEditor的容器中去,讓W(xué)angEditor去控制界面的dom操作。
- 實(shí)現(xiàn)ControlValueAccessor,讓這個組件支持Angular的表單驗(yàn)證。
- 實(shí)現(xiàn)ngOnDestroy,組件在銷毀的時候,調(diào)用WangEditor的destory
4.使用組件
<q-wang-editor [height]="550"></q-wang-editor>
5.效果預(yù)覽

6.最后
一個WangEditor的Angular組件封裝就基本完成了。如果需要更多功能,比如圖片上傳,等可以再根據(jù)自己的需求增加功能即可
到此這篇關(guān)于Angular封裝WangEditor富文本組件的文章就介紹到這了,更多相關(guān)Angular WangEditor富文本組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解AngularJs中$sce與$sceDelegate上下文轉(zhuǎn)義服務(wù)
這篇文章給大家詳細(xì)介紹了AngularJs提供的嚴(yán)格上下文轉(zhuǎn)義服務(wù)$sce與$sceDelegate,文中介紹的很詳細(xì),有需要的朋友們可以參考借鑒。2016-09-09
對比分析AngularJS中的$http.post與jQuery.post的區(qū)別
這篇文章主要給大家對比分析AngularJS中的$http.post與jQuery.post的區(qū)別,十分的詳細(xì),是篇非常不錯的文章,這里推薦給小伙伴們。2015-02-02
Angular 4依賴注入學(xué)習(xí)教程之簡介(一)
依賴注入式AngularJS的重要特性之一,依賴注入簡化了Angular解析模塊/組件之間依賴的過程。下面這篇文章主要給大家介紹了關(guān)于Angular 4依賴注入基礎(chǔ)的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06
angular6.0開發(fā)教程之如何安裝angular6.0框架
這篇文章主要介紹了angular6.0開發(fā)教程之如何安裝angular6.0框架,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
AngularJS實(shí)現(xiàn)表單元素值綁定操作示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)表單元素值綁定操作,結(jié)合具體實(shí)例形式分析了AngularJS針對表單元素屬性相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
解決Angular.Js與Django標(biāo)簽沖突的方案
AngularJS和django的模板都是用{{}}來引用變量的,這就導(dǎo)致了沖突,所以這篇文章主要就給大家介紹了如何解決Angular.Js與Django標(biāo)簽沖突的方案,有需要的朋友們可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2016-12-12

