为什么这个 Angular JS 过滤器会去除空格? [英] Why is this Angular JS filter removing spaces?

查看:32
本文介绍了为什么这个 Angular JS 过滤器会去除空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

HTML:

"

<textarea name="textarea" rows="10" cols="50" ng-model="encodeText" placeholder="输入或粘贴要编码的消息."></textarea><p>过滤后的输入:{{ encodeText |编码}}</p>

JS:

(function(){var app = angular.module('myApp', []);app.filter("encode", function() {返回函数(输入){如果(输入){//文本处理控制台日志(输出);返回输出;}};});

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

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

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

过滤输入:.- -... -.-.-.....-.

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

解决方案

p格式化您的输出.试试:

{ encodeText |编码 }

I'm making a tiny app to learn Angular, and have made a custom filter for text processing. Everything is working fine, except that in cases when my output text has multiple spaces, these get automatically reduced to a single space, which I don't want. The issue is not in the function I've defined, because I'm logging the output, which does not have the problem.

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;
      }
    };
 });

It is a Morse Code exercise. So console log output is:

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

With two spaces. On the page I see:

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

The only suggestion I'm seeing from Googling is using ng-trim="false" on the textarea, which has no effect. It seems the trimming is happening within the filter itself. What is causing this and how do I disable it? Code repo

解决方案

The pformat your output. Try:

<pre>{ encodeText | encode }</pre>

这篇关于为什么这个 Angular JS 过滤器会去除空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆