关于angularjs+ueditor的一个bug
来源:互联网 发布:远程办公软件 编辑:程序博客网 时间:2024/05/22 20:24
最近在写一个可配置,灵活度比较高的问卷调查系统,实现后台配置动态生成问卷,使用到了angularjs和百度的富文本编辑器ueditor,为了实现文本内内容的绑定在网上找了一段集成代码,其实就是一个指令。使用过程中发现了一个不容易发现的bug,就是在编辑器中插入图片之后不再进行其他内容的插入,此时图片地址是错误的。以下是修改以后的代码:
angular-ueditor.js
(function() { "use strict"; (function() { var NGUeditor; NGUeditor = angular.module("ng.ueditor", []); NGUeditor.directive("ueditor", [ function() { return { restrict: "C", require: "ngModel", scope: { config: "=", ready: "=" }, link: function($S, element, attr, ctrl) { var _NGUeditor, _updateByRender; _updateByRender = false; _NGUeditor = (function() { function _NGUeditor() { this.bindRender(); this.initEditor(); return; } /** * 初始化编辑器 * @return {[type]} [description] */ _NGUeditor.prototype.initEditor = function() { var _UEConfig, _editorId, _self; _self = this; if (typeof UE === 'undefined') { console.error("Please import the local resources of ueditor!"); return; } _UEConfig = $S.config ? $S.config : {}; _editorId = attr.id ? attr.id : "_editor" + (Date.now()); element[0].id = _editorId; this.editor = new UE.ui.Editor(_UEConfig); this.editor.render(_editorId); return this.editor.ready(function() { _self.editorReady = true; _self.editor.addListener("contentChange", function() { //此处设置一个延迟,防止图片还没有从服务器返回,从而获取到的是loading图片 setTimeout(function() { ctrl.$setViewValue(_self.editor.getContent()); if (!_updateByRender) { if (!$S.$$phase) { $S.$apply(); } } _updateByRender = false; }, 50) }); if (_self.modelContent && _self.modelContent.length > 0) { _self.setEditorContent(); } if (typeof $S.ready === "function") { $S.ready(_self.editor); } $S.$on("$destroy", function() { if (!attr.id && UE.delEditor) { UE.delEditor(_editorId); } }); }); }; _NGUeditor.prototype.setEditorContent = function(content) { if (content == null) { content = this.modelContent; } if (this.editor && this.editorReady) { this.editor.setContent(content); } }; _NGUeditor.prototype.bindRender = function() { var _self; _self = this; ctrl.$render = function() { _self.modelContent = (ctrl.$isEmpty(ctrl.$viewValue) ? "" : ctrl.$viewValue); _updateByRender = true; _self.setEditorContent(); }; }; return _NGUeditor; })(); new _NGUeditor(); } }; } ]); })();}).call(this);该指令通过监听ueditor的contentChange事件来给ng-model变量赋值,从而达到数据绑定的目的。但是如果内容是图片就会有一个请求服务器的间隔。如果直接在文本框输入一张图片,打印出这个model的值,可以发现值是loading图片的地址,我猜测ueditor在请求之前给content赋值为loading图片地址,待图片地址从服务器返回后进行覆盖。所以如果在图片地址还没返回之前取content的值是错误的,所以我设置了一个50ms的延迟,虽然不能从根本上解决,但是主要保证图片请求服务器并返回地址的时间少于50ms就没问题,另外这样的另一个不足就是不管图片还是文字内容变化都会有50ms延迟。还有就是这个bug只会在最后一个输入的是图片才会出现,因为如果你紧跟着输入文字会出触发contentChange事件去获取content,此时图片地址已经返回,获取的就是正确的地址。
0 0
- 关于angularjs+ueditor的一个bug
- angularjs中使用ueditor遇到的一个问题
- 关于ueditor编辑器双引号 “”转义BUG的修复
- 关于memagent的一个bug
- 关于缓冲区的一个BUG
- 一个关于 UIPickerView 的 bug
- 关于一个bug的解决
- 关于SwipeMenuListView的一个BUG
- 关于CAD2008的一个bug
- 关于EditText的一个Bug
- 关于ueditor 1.4.3 上传小于1M图片时不压缩的bug修正
- 使用ueditor时发现的bug
- 修复UEditor拖放大小的bug
- 关于ICMP Redirect路由的一个不是bug的bug
- Microsoft的一个关于DirectX3D的BUG
- 一个关于微软的_toupper的bug!
- 一个关于ie的bug——双边距bug。
- 一个关于临时对象的BUG(上)
- 【NAS】Samba常用命令
- 解析解与数值解
- 聊聊并发——Java中的阻塞队列
- MySQL批处理sql文件时data too long for column问题
- [Verilog]ADC0809驱动程序
- 关于angularjs+ueditor的一个bug
- 科比罚篮,武器命中和霍金劫数
- JavaScript window.location IE谷歌跳转路径不兼容
- YARN - 比较 YARN 与 MapReduce 1
- 【WPF】使用Popup控件做浮窗/提示框
- 0. spark源码学习分享 前言
- 1295: Fengsensei的测试
- iOS APP删除系统相册中选中的图片-姬云鹏
- hpuoj-1010-QAQ的序列价值【状压】【数学组合】