【问题标题】:rich text with non editable content, angularjs具有不可编辑内容的富文本,angularjs
【发布时间】:2018-01-22 10:25:51
【问题描述】:

当我需要在富文本中添加不可编辑的 html 时,我使用指令 textAngular 来格式化我的电子邮件通知文本并坚持执行任务。我在 textAngular 指令上添加了我的自定义指令。在我的自定义指令的帮助下,我将字符串中的特殊字符替换为带有参数contenteditable='false' 的 html span 标签,但是这个参数不起作用并且内容仍然可以编辑。

在这种情况下,我有两个问题:

  1. 如何在内容 textAngular 指令中设置不可编辑的 html?
  2. 如何将我的变量连接到想要放置的字符串(目前它总是连接到字符串的末尾)

感谢您的帮助。

Plunker我的问题

我的自定义指令:

app.directive('removeMarkers', function () {

return {
    require: "ngModel",
    link: function (scope, element, attrs, ngModel) {

       element.bind('click', function (e) {
                var target = e.target;
                var parent = target.parentElement;
                if (parent.classList.contains('label-danger')){
                    parent.remove()
                }
            });

        function changeView(modelValue){
            modelValue= modelValue
                .replace(/\{{/g, "<span class='label label-danger' contenteditable='false' style='padding: 6px; color: #FFFFFF; font-size: 14px;'>")
                .replace(/\}}/g, "<span contenteditable='false' class='remove-tag fa fa-times'></span></span>&nbsp;");
            ngModel.$modelValue= modelValue;

            console.log(ngModel)
            return modelValue;
        }
        ngModel.$formatters.push(changeView);
        }
    }
});

HTML

  <select class="form-control pull-right"
                style="max-width: 250px"
                ng-options="label.name as label.label for label in variables"
                ng-change="selectedEmailVariables(variable)"
                ng-model="variable">
            <option value="">template variables</option>
   </select>

  <div text-angular remove-markers name="email" ng-model="data.notifiation"></div>

【问题讨论】:

  • 开发者似乎很不情愿添加这个功能。显然,ckeditor 是一个不错的选择。

标签: javascript html angularjs textangular


【解决方案1】:

1) 您不能在您的元素上添加simply use contenteditable='false' 属性,因为过去textAngularstrips it. To enable it 我们必须下载textAngular-sanitize.min.js 并编辑允许的属性数组。这以J=f("abbr,align, 开头,您只需将contenteditable 添加到此逗号分隔列表中即可。

2) 要将模板插入到光标位置,您可以use wrapSelection method within editor scope,这样可以工作:

$scope.selectedEmailVariables = function (item) {
    if (item !== null) {
          var editorScope = textAngularManager.retrieveEditor('editor1').scope;
          editorScope.displayElements.text[0].focus();
          editorScope.wrapSelection('insertHtml', 
          " <span class='label label-danger' contenteditable='false' style='color: #FFFFFF; font-size: 14px;'>" + item + "<span contenteditable='false' class='remove-tag fa fa-times'></span></span> ", true);
    }
};

这是working plnkr

P.S.:textAngular 似乎是一个不错的编辑器,但它缺少一些功能,而且我个人不喜欢它的文档。您必须通过 github 上的问题收集大量信息。现在切换到其他替代编辑器,但将来可能会回到它。

【讨论】:

  • @antonyboom 很高兴它对您有所帮助。我们不得不搬到CKEditor,因为它有更好的字体管理,还有很多插件(图片上传,工具栏下拉等),文档对我来说似乎更清楚(但这只是我的想法 :))。这里唯一的问题是找到/实现一个好的指令包装器来使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-04
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
相关资源
最近更新 更多