【问题标题】:Adding <p> to an unformated text in angular将 <p> 添加到角度的无格式文本
【发布时间】:2017-07-21 12:43:57
【问题描述】:

我正在粘贴如下纯文本,就在 Summernote 的文本框中,并对其进行处理以删除所有 html 格式。但我需要再次处理它以更新模型,将&lt;p&gt; 添加到段落并删除空行。

这就是我所拥有的

Example Test
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.

Nulla consequat massa quis enim.

还有……

这是我需要的:

<p>Example Test</p>
<p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
<p>Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.</p>
<p>Nulla consequat massa quis enim.</p>

这是我的看法:

<div summernote config="options" ng-model="post.description" on-paste="snPaste(evt)"></div>

这是我用来粘贴的脚本:

$scope.snPaste = function(e) {
    //First define the clean content
    var cleanUp = e.originalEvent.clipboardData.getData('text');

    //Have it wait for 0.5 seconds to update the pasted
    setTimeout(function () {
        $scope.$apply(function () {
            $scope.post.description = cleanUp; //Here I update the model.
        });
    }, 500);
};

【问题讨论】:

    标签: angularjs wysiwyg copy-paste summernote


    【解决方案1】:

    结果如下:

    这是对控制器的调整:

    angular.module('summernoteDemo', ['summernote'])
      .controller('CodeCtrl', function($scope) {
        $scope.snPaste = function(e) {
          //First define the clean content
          clipboardData = e.originalEvent.clipboardData || window.clipboardData;
          var cleanUp = clipboardData.getData('Text');
    
          //Have it wait for 0.5 seconds to update the pasted
          setTimeout(function() {
            $scope.$apply(function() {
              $scope.post.description = '<p>' + cleanUp.replace(/\r\n\r\n|\n\n/g, '</p><p>').replace(/\r\n|\n/g, '</p><p>') + '</p>'; //Here you update the model.
            });
          }, 500);
        };
      });
    

    这是一个有效的 JSFiddle,http://jsfiddle.net/rekrah/nkosyafs/

    【讨论】:

    • 你好朋友,我尝试粘贴纯代码,但没有成功,它只是在开头和结尾添加了&lt;p&gt;,而不是在结尾和开头之间的行中。
    • 我以为你只是在粘贴文本。将您要粘贴的内容发送给我。
    • @AbielMuren 您在这方面需要进一步的帮助吗?
    • 对不起,是的,我需要它,尝试粘贴上面的示例,由于某种原因,只在开头和结尾添加了&lt;p&gt;。如果此站点,请尝试复制粘贴列表,例如侧面菜单中的列表。它会删除格式并添加&lt;p&gt;,但只是在开头和结尾。
    • @AbielMuren 看起来像浏览器兼容性问题,我更新了我的答案和 JSFiddle。现在看看,让我知道。
    猜你喜欢
    • 2012-11-30
    • 1970-01-01
    • 2017-02-06
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 2022-01-11
    相关资源
    最近更新 更多