1

我正在制作一个小应用程序来学习 Angular,并为文本处理制作了一个自定义过滤器。一切正常,除了在我的输出文本有多个空格的情况下,这些会自动减少为一个空格,这是我不想要的。问题不在我定义的函数中,因为我正在记录没有问题的输出。

HTML:

<textarea name="textarea" rows="10" cols="50" ng-model="encodeText" placeholder="Type or paste in a message to encode." ></textarea>
<p>Filtered input: {{ encodeText | encode }}</p>

JS:

(function(){
  var app = angular.module('myApp', []);
  app.filter("encode", function() {
    return function(input) {
      if (input) {
        //text processing
        console.log(output);
        return output;
      }
    };
 });

这是一个摩尔斯电码练习。所以控制台日志输出是:

.- -... -.-.   -.. . ..-. 

有两个空格。在我看到的页面上:

Filtered input: .- -... -.-. -.. . ..-.

我从谷歌上看到的唯一建议是在 textarea 上使用 ng-trim="false",这没有任何效果。似乎修剪发生在过滤器本身内。是什么原因造成的,我该如何禁用它? 代码仓库

4

2 回答 2

7

默认情况下,HTML 不保留多个空格(将它们折叠成 1 个空格)。<p>向标签添加样式以保留空格...

<p style="white-space: pre">Filtered input: {{ encodeText | encode }}</p>
于 2014-05-30T23:08:56.037 回答
2

输出的p格式。尝试:

<pre>{ encodeText | encode }</pre>
于 2014-05-30T23:09:50.243 回答